How to re-commit a past commit if someone overwrote my commit

You can create a branch out of your old commit and then follow the git process to merge changes from master to your branch and handle merge conflict.

Below steps for create a branch out of your old commit and checkout the same.

git branch branchname <sha1 of your old commit>
git checkout branchname  

or

git checkout -b branchname <sha1 of your old commit>

The easiest way to fix this is usually, as Code_Ninja suggested, to use git cherry-pick to re-commit your changes. Usually, the method recommended by Venkataraman R won't work because Git will tell you that there is nothing to merge. (If it does work, it's OK too.) The exact details depend on the situation. For posterity, though, let's look at the most common way that this happens.

Someone got merge conflict and while merging he lost my all changes.

This is very easy for the "someone" to do. Let's give "someone" a name; let's call him Carl the Careless. Carl runs:

git merge carls-feature

or perhaps even:

git pull

(though I recommend that any beginner, like Carl here, avoid git pull—it's better that he run git merge directly, so that he can get an idea about what he's doing).

In any case, Carl now sees this:

Auto-merging somefile.ext
CONFLICT (content): Merge conflict in somefile.ext

Poor Carl panics, searches the web (perhaps even StackOverflow) for advice, and rather than looking at, say, How to resolve merge conflicts in Git,1 Carl just runs:

git checkout --ours somefile.ext
git add somefile.ext
git commit

Carl has just wiped out your work!

Remember, the goal of a merge is to combine work. Git will try to do this on its own, but sometimes Git is not able to complete the process by itself. In these cases, Git stops and gets help from its human operator.

This human operator—you, if you're the one with the merge conflict!—is in full, complete, 100% control of the merge result. Git will believe you, even if you tell it that the correct result is to ignore the other guy's work entirely and just take your version.

(Note that even if Git thinks it has correctly merged two people's different changes, Git does not always get it right. It's still your responsibility to check that Git got it right. It's a good idea to have some sort of thorough test system you can use. Git is just following simple text-combining rules, that happen to work really well for many cases.)


1The accepted answer here recommends using git mergetool with vimdiff as the tool. I happen to dislike git mergetool, and I recommend reading through the other answers too, and experimenting to see what works best for you. But if git mergetool works well for you, that's fine. Note that git mergetool can use other merge tools than vimdiff; if you have a merge tool you prefer, git mergetool is likely to be able to run it. The Pro Git book has additional advice, including a chapter on advanced merging.


You can do that typing the following commands:

$ git reflog

then select the ID of the commit that you want to retrieve.

Then type the following command:

$ git cherry-pick <'ID'>

Instead of <'ID'> enter the ID from the above reflog.

Then you will have the changes of that commit.

Now check if anything is still remaining by:

$ git status

If anything is in unstaged commit, then add it by the following commands and commit:

$ git add -A //Any other option or the name of the file with the path that you want to commit
$ git commit -m "Your message here for that commit"
$ git push

I hope this answer is what you are expecting for.

Tags:

Git