Git tricks I always forget about

Git is chok-full of features, so much so that you’ll probably forget about how to do a lot of things you know to be possible if you are not using said features on a daily basis. Outside of the regular add, commit, checkout, rebase, push, pull and merge commands that you might use everyday, there are a lot of edge cases and rare situations where you’ll find yourself reading the documentation and the manpages to find out how to do something that sounds simple with an obscure command you didn’t memorize with an even more obscure parameter that you didn’t know existed.

The following article is a list of tricks and commands that I keep forgetting about in increasing order of complexity and coolness. I’ll try to keep this article up-to date with increasingly cooler tricks.

Update the remote informations

Update the tracking informations from your remote:

git remote update

Push the local tag informations

Useful when you have created a tag locally but it’s not showing up on your remote:

git push --tags

Unstage a file

Remove a file that was mistakenly added:

git reset -- <filename>

Revert a file

Revert a file to its previous state, can be used to restore a deleted file:

git checkout -- <filename>

from a tag:

git checkout <tag> -- filename

from a specific branch:

git checkout <branch> -- filename

from the head:

git checkout HEAD -- filename

from the one just before the current head:

git checkout HEAD^ -- filename

Delete last commit

Delete the last commit you made:

git reset --soft HEAD~1

Read more about it here.

Add a missing file to the latest commit

Add a file you forgot to add in the latest commit:

Add the missing file:

git add <filename>

Ammend the latest commit:

git commit --amend -C HEAD

Replace -C by -c if you want to edit the commit message.

See a graph of the commit log

Self explanatory:

git log --graph

Get the number of commits made by each contributor

Self explanatory too:

git shortlog -s -n

Resolve a conflict with a specific merge tool

Resolve a merge conflifct with a specific mergetool:

git mergetool --tool=<mergetool>

Moving the last X commit on a branch to a new branch

git branch newbranch
git checkout oldbanch
git reset HEAD~X
git stash 

Change author info of several commits

GitHub has this neat script that does the job.



Enter your desired user name/password and after your comment has been reviewed by an admin it will be posted and your account will be enabled. If you are already registered please login before posting.