Git Tips And Tricks

Various Git tips I learnt along the way

Show local and remote branches

1
2
git branch
git branch -r

See branches sorted by the last commit

1
git branch --sort=-committerdate

Note: some git commands require installing globally NPM module diff-so-fancy.

1
npm i -g diff-so-fancy

See the diffs with the log

1
git log -p

Get the file from another branch

1
2
3
git checkout <branch name> <filename>
# even from another commit
git checkout <sha> <filename>

Checkout previous branch

Just like cd - goes to the previous working directory, git checkout - goes to the previously checkout branch.

Git CLI commands

I use git-extras to perform all my Git commands from the terminal.

Remove untracked files

Sometimes you get a bunch of generated files in the repo, and would like to remove them.

1
2
3
4
5
6
# first, see which files will be removed
$ git clean -d -n
# you can go through each file and confirm you want it deleted
$ git clean -d -i
# or if you are sure, delete them
$ git clean -d -f

See more

Read my other Git blog posts, starting with Git aliases.

Related posts