I have 2 commits that I did not push:
$ git status
# On branch master
# Your branch is ahead of 'faves/master' by 2 commits.
How can I roll back my first one (the oldest one), but keep the second one?
$ git log
commit 3368e1c5b8a47135a34169c885e8dd5ba01af5bb
...
commit baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
...
From here:
http://friendfeed.com/harijay/742631ff/git-question-how-do-i-rollback-commit-just-want
Do I just need to do:
git reset --hard baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
That is?
Best Answer
The safest and probably cleanest way to go is to rebase interactively.
Or,
From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.
You can revert a commit with
git revert
but its going to add more commit messages to the history, which may be undesirable. Use the-n
parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.If the two commits you're working with here affect the same file(s), you may see a merge conflict.
Resetting the repository with
git reset --hard
should be done with care, as it cannot be undone.Rewriting history should be done with care.