Deprecation Notice:This article was written more than a year ago which means that its
contents might no longer be up-to-date. If you notice any inaccuracies, feel free to leave a message
in the comments below and we will look into updating the article soon as we can. Thank you for your support.
Table of Contents
10 Useful Git Commands
Here at GeekBitZone we use extensively Git as our version control system.
As a matter of fact, even our statically generated website is under Git’s version control.
Below are ten useful commands that we use on a daily basis.
git commit —amend
$ git commit —amend
Amend commit message. Useful if you, for example, want to change your latest comment but do want to make another commit.
git fetch —prune
$ git fetch —prune
Update list of remote branches and from your local repository, remove those that not longer exist remotely.
If you work in a large team the list of branches usually grows over time.
If you do not tell Git to perform some house cleaning every now and then, your local repository list will soon be
cluttered with many obsolete branches.
git branch -vv
$ git branch -vv
Print list of local branches and also include any remote branches that have not yet been checkout out.
git reset —hard origin/<branch>
$ git reset —hard origin/mybranch
Reset current branch to remote HEAD. Very useful if you want restore your local branch to its initial (remote) state.
Always make sure that your remote list is up-to-date by issuing the git fetch origin command first.
git branch -D <branch>
$ git branch -D mybranch
Force delete local branch. A less aggressive git branch -d <branch> command can also be used,
provided that there are no un-merged changes left in the branch.
git push —delete origin <branch>
$ git push —delete origin mybranch
Delete remote branch. Use with care. Only run this command when you are absolutely certain that the remote branch is no
git branch -m <name>
$ git branch -m mynewname
Rename local branch. This command renames the current branch, so do ensure that you are working on the correct branch.
git rebase -i HEAD~<n>
$ git rebase -i HEAD~3
Rebase the latest number of commits. This is useful if you, for example, want to squash multiple commits into one
single commit before merging the changes back to the master branch. When the rebase dialog pops up,
it is recommended to pick (p) the first commit and squash (s) every subsequent commit.
$ git stash
Store un-committed changes in a temporary “buffer”. Useful if you, for example, want to checkout another branch or
pull the latest remote branch without having to commit your local changes. Once the changes have been stashed your
branch will appear “clean”.
git stash pop
$ git stash pop
Restore changes from the temporary “buffer” onto the current branch.
The stash acts on a First In First Out (FIFO) basis, so if you have multiple stashes,
the latest one will always be applied first.
These are GeekBitZone’s ten favourite Git commands. Which ones do You use on a daily basis?