Is there a difference between “git reset –hard hash” and “git checkout hash”?

This answer is mostly quoted from my answer to a previous question: git reset in plain english. The two are very different. They result in the same state for your index and work tree, but the resulting history and current branch aren’t the same. Suppose your history looks like this, with the master branch currently … Read more

How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?

Getting back in synch after a pushed rebase is really not that complicated in most cases. git checkout foo git branch old-foo origin/foo # BEFORE fetching!! git fetch git rebase –onto origin/foo old-foo foo git branch -D old-foo Ie. first you set up a bookmark for where the remote branch originally was, then you use … Read more

I need to pop up and trash away a “middle” commit in my master branch. How can I do it?

Rebase or revert are the options. Rebase will actually remove the commit from the history so it will look like that second commit never existed. This will be a problem if you’ve pushed the master branch out to any other repos. If you try to push after a rebase in this case, git will give … Read more

What is difference between ‘git reset –hard HEAD~1’ and ‘git reset –soft HEAD~1’?

git reset does know five “modes”: soft, mixed, hard, merge and keep. I will start with the first three, since these are the modes you’ll usually encounter. After that you’ll find a nice little a bonus, so stay tuned. Let’s assume you have a repository with a history akin to this: 7e05a95 (HEAD -> main) … Read more