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

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



Just faced an issue where someone overwritten my whole commit and there are lot of other commits on top of this, now I have to re-commit that particular commit. Any help would be appreciated.



Someone got merge conflict and while merging he lost my all changes. I don't know what he did, but my changes are not there. IN history my commit is there but If I am seeing above his merge my changes are reverted.



Now I cannot revert his merge commit because it contains lots of files as compare to me also I don't want to change each file one by one manually. Is there any way to get back my changes without effecting other's.





Was this person using TortoiseGit? I've seen this kind of thing happen with TortoiseGit. It gives you the option to uncheck things you don't want in the merge, and some people cluelessly uncheck all the files they don't recognize as having changed (because the other side of the merge changed them).
– Ryan Lundy
Sep 21 '18 at 7:56




3 Answers
3



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.


<'ID'>



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.



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.


git cherry-pick



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).


git pull


git merge



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.


git mergetool


vimdiff


git mergetool


git mergetool


git mergetool


vimdiff


git mergetool





Thanks for so much information, by creating branch worked for me, but I am agree with you that I should follow cherry picking method in this type of case. Hence changing my accepted answer.
– CuriousGeek
Sep 5 '18 at 5:53





To elaborate on why merging in a branch of the commit shouldn't work, it's because typically the commit is already in the history (in whatever shared branch you're using). You can't merge in a commit that is already there. The cherry-picking approach would work instead because it creates an entirely new commit (which is simply duplicating the changes). Git has to be used to not merging in commits that are already there because branches are really just a pointer to some commit (which in turn points to its parents, going allll the way back). So branches are supposed to share many commits!
– Kat
Sep 10 '18 at 18:44



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>





thanks @Venkataraman, But we don't have permissions for creating branch. Are you talking about local branch?
– CuriousGeek
Sep 5 '18 at 4:36





@CuriousGeek, Yes. I am talking about local branch. You can later set-upstream for this local branch.
– Venkataraman R
Sep 5 '18 at 4:41





As an aside, git branch (and similar like git checkout -b <new branch name>) is always a local branch. Almost every kind of change you'd typically make in git is local until you push it to a remote repo. You can always choose to keep things locally only or to only push certain branches.
– Kat
Sep 10 '18 at 18:41


git branch


git checkout -b <new branch name>



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)