Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 976
Sombir Kumar Avatar asked Sep 05 '18 03:09

Sombir Kumar


People also ask

How do I restore an old commit?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .

How do I go back to a commit in history?

Go back to the selected commit on your local environmentUse git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> . Don't forget the final ' .

How do you go back to previous commit and push?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I undo a deleted commit?

The process for recovering a deleted commit is quite simple. All that is necessary is to use `git reflog` to find the SHA value of the commit that you want to go to, and then execute the `git reset --hard <sha value>` command.


2 Answers

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.

like image 116
Code_Ninja Avatar answered Sep 22 '22 20:09

Code_Ninja


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.

like image 34
torek Avatar answered Sep 19 '22 20:09

torek