git revert
undoes changes by adding new commits which apply reverse changes of what was done in previous commits. It's useful when you want to undo changes without re-writing the history of branches which you have pushed publicly and which other may already have pulled into their own repositories.
If that's not your case, I suggest to undo changes using more intuitive techniques, such as git reset
. Let's say your master branch had commits A, B, C, and you accidentally committed D and E on top of that.
master
A <-- B <-- C <-- D <-- E
If D and E were your unwanted, superfluous commits, you could simply do:
Go on your local master branch:
git checkout master
Make it point to a particular commit using --hard
. This is a very general-purpose command, the commit can be anything in your object base, it doesn't necessarily have to be a commit in master's history. In your case:
git reset --hard <sha1 of C>
master
A <-- B <-- C
Then push to your repo using force, since you have re-written history and it is thus non-fast-forward:
git push origin master -f
Then, checkout refactor and keep working on it. If you want to pickup from where your master branch was pointing (i.e. with the extra superfluous commits), you can use git reset
to set it there:
git checkout refactor
git reset --hard <sha1 of E>
master refactor
A <-- B <-- C <-- D <-- E
This is optional, of course, you could decide to use another way such as branching refactor from the new master and adding the previous modifications manually, or by cherrypicking, etc.
Then just keep adding commits to refactor
, then merge to master and push master:
git checkout master
git merge refactor
git push origin master
master
refactor
A <-- B <-- C <-- D <-- E <-- F
There, it's still a very basic workflow. My suggestion would be to use a tool like gitk
:
gitk --all
as often as possible to see what's happening on your different branches.