Git FAQs I Encounter Regularly

Sanket Patel on September 12, 2018

Git is much more than just push/pull of your code. In this post I will discuss other supporting commands and tricks to handle some scenarios I ha... [Read Full]
markdown guide

I think there is a small mistake in that

git checkout new-branch-name

should probably be

git checkout -b new-branch-name

The first command will only work if new-branch-name already exists which would probably defeat the purpose :)


Yes, I'm Sorry. You are correct @ozzyaaron . Thanks for pointing out. Updating the post.


No worries! I know I don't make mistakes, but many other people do :P


For #2, another option is git checkout -- <paths>. This will replace the contents of all <paths> with whatever is in the Git index at those paths. If you have not staged anything for commit, then this will replace the files with the contents from the most recent commit in your current branch.

For #3, as always when erasing commits, be careful that the commit you are about to erase has never left your local machine! If it has, then it's possible someone else has based work off the commit and erasing it would cause a conflict on their machine.

If you have only one commit that you want to erase, you can easily reset to the previous commit using the shorthand notation HEAD^. Here HEAD references the current commit, and ^ means "the first parent of this commit". This is more convenient if you only want to step back by one commit as you don't need to look up the exact SHAs.

You can also use symbolic notation if you want to step more than one commit back. Instead of ^ use ~n where n is the number of steps to take. HEAD~1 means "one step back from HEAD", while HEAD~3 means "three steps back from HEAD". This is also useful if you know you want to erase a certain number of commits, as you can just tell Git to walk backwards a certain number of steps instead of needing to look up the SHAs.


Great tips here, to add on to top #3, instead of having to checkout into another new branch to retain your change, you can actually just do

git reset --soft head~1 (~1 being one commit, so obviously this varies)

Basically, --hard reverts your commits and discards your change, but whereas --soft reverts your commits but retains your change, so you can just checkout to another branch and commit there.


That seems nicer approach. Will give a try. Thanks

code of conduct - report abuse