Search code examples
gitversion-controlgit-revert

Git Revert a few commits (including merges) then recommit them later


I have a staging server where I update the code using git pull from the git server. One detail here which may or may not be relevant is that at some point it started happening that every time I pull it asks me to merge. I haven't had enough time to figure out why yet. Anyways, if I look at git log there is a merge after every pull. Anyways, not to the real question. I just need to temporarily revert 2 previous commits (with 2 corresponding merges). I am pretty confident in how to do this using git revert the 2+2 = 4 hashes that appear in the git log and then committing.

The question is that I would like to then re-commit the reverts later in the future, since we will need these changes later but just not now. Do I need to do anything special so that I don't lose the commits that I revert?


Solution

  • revert is probably NOT what you want; revert adds a new commit that undoes a set of changes. reset is more like it, which is for modifying branch histories, but read on before doing anything brash.

    First thing I'd suggest is to try using pull -r to automatically rebase your local changes onto the updated HEAD of the branch (i.e. the remote copy of the branch).

    You could also start an interactive rebase with rebase -i origin/mybranch. Rebase is git's most powerful feature that allows you to rewrite history. It's also mildly dangerous, so beware.

    Tutorial on rebase: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

    No matter what you do to your local git directory, reflog will let you see what previous states (commits) of your repo. It's saved my butt once or twice when I accidentally "lost" a commit when I screwed up a rebase or a reset.