45
votes

I've committed a bunch of changes to a repository, and they were reverted by someone else (they compile on windows but not on linux). I think that the changes are still in the history, but how can I get those changes back, fix them, and then resubmit?

4

4 Answers

79
votes

Have you tried reverting the revert?

They reverted your commit using

git revert <your commit id>

The revert itself is a commit and has its own commit id. So now you need to do

git revert <the commit id of his revert-commit>
23
votes

You can try reverting the reverts, using git revert. You can also restore the files from your commit using git checkout. Or you can use git cherry-pick -n to re-apply them and change them. You can create a new branch from your commit where you apply the changes using git branch. The possibilities are (almost) endless. :)

3
votes

The other answers here address how to revert or cherry-pick your commits, but it sounds from the question as if you also need to know how to find your commits, so here's some guidance on that.

If you just run git log you should see a list of the commits in the history of your current branch, starting with the most recent. Each commit has an identifier technically known as the object name (or "SHA1sum of the commit") that looks like this:

commit d2d434beeb03e4ee648ca7ca2a1ea1ed09077306

... followed by the author name, date and a summary of the changes that were introduced by that commit. When you're specifying the object name for git revert or git cherry-pick you can give it d2d434beeb03e4ee648ca7ca2a1ea1ed09077306 or just enough characters from the beginning of the object name to be unambiguous (e.g. d2d434bee)

git log has many options that can help you track down your commits, like --before, -S, etc. but in this case you particularly might need --author=MyLastName.

A graphical git tool that can present the history nicely to you and is available on every platform is gitk --all. If you click on a commit that you'd like to revert, you can copy-and-paste its object name from the "SHA1 ID" field in the middle of the window.

1
votes

You can also do like similar situation answer https://stackoverflow.com/a/10415744/704008 from @MarkLongair. Answer is better suited for multiple commits and if you don't want mess in git log or source control history of revert & then again revert of revert.

git revert abcd123
git revert --no-commit wxyz789
git commit --amend -m"you edit commit message"

You can also have concise in one line for two or more revert commits like :

git revert abcd123 wxyz789 --no-commit 
git commit --amend -m"you edit commit message"

... and then write an appropriate commit message describing the combined effect of reverting both commits.

All this will appear as single commit with given message & not multiple revert commit in git log or above source control logs(like bitbucket/TFS/JIRA git integration).