Git Hacks

Git Comments

Below are some of the Git commands that are not well known but I use them frequently and I find them to be useful -

Adding Committed Files to Gitignore

1. Add /path/to/file to .gitignore
2. git rm --cached /path/to/file

Removing Local Commits

One way to do this is to delete the local branch altogether and pull from origin again.

1. git branch -D <local-branch>
2. git checkout origin/<branch> -b <local-branch>

Find out the added commits in this branch

If you create many branches, you usually tend to forget what were the added commits in the local branch.

git diff <local-branch> ^origin

This command returns all the commits that are there in local-branch but not there in origin.

Owners of a file

git blame path/to/file

Merge a commit from another branch

git cherry-pick <commit>

Rename a local branch

To rename the current branch - git branch -m <newname> 
To renemae any branch - git branch -m <oldname> <newname>

I’m sure there are more, but I’ll add them as I think of them!

Kaushik Rangadurai

Code. Learn. Explore

Share this post