3

I am working a project, and noob to git architechture. I have accidentally commited a few changes to mainline, now I want to revert them back to what is in the main repository.

Now I am on a new branch, but those commits are still there in the mainline branch. How do I do that?

And what exactly is HEAD, I am very much confused.

There are 3 commits i need to revert. So for one of them I did "git revert ". Now when I do "git revert HEAD~3" it shows I need to merge changes and commit n all. But I dont want those changes, that is why I reverted.

I might sound very foolish and confusing, but I am very new to this, and dont want to loose any changes, as its a very crucial project and rather dont want to mess up the mainline branch as well.

Filburt
  • 17,626
  • 12
  • 64
  • 115
user2696258
  • 1,159
  • 2
  • 14
  • 26
  • What are we looking at here, in terms of the actual commit history? Could you show us a picture of that (sensitive bits left out, of course), or could you use `git log --graph --oneline --annotate --all` so that we can get a clearer picture of the commits you need to remove? Now, the reason they'd appear on a new branch is due to them being at the tip of your mainline branch when you decided to branch off, but that's fixable. Let's first work on getting you in a state where we could fix this. – Makoto Aug 26 '15 at 14:59
  • possible duplicate of [Revert to a previous Git commit](http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit) – Danielson Aug 26 '15 at 15:04

1 Answers1

0

I'm assuming your git topology looks something like this:

---A---B---C---D master
                 newbranch

You'll want to git reset your master branch back like this:

git checkout master
git reset --hard HEAD~3

Your topology should now look like this:

---A---B---C---D newbranch
    \
     master

To answer your question about HEAD, it refers to the tip of the current branch you're on. If you're on master, then HEAD is A; if you're on newbranch, then it's D.

See the following questions:

Community
  • 1
  • 1
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • thanks a lot! One more thing, I did "git revert " so it created one more commit of reverting changes, and I have some unstaged files. So I will need to do git reset --hard HEAD~4 right? – user2696258 Aug 26 '15 at 15:14
  • That depends whether you did the revert on master, or newbranch, or both. `git revert` just reverts a single commit by applying the reverse of it in a new commit. So it's likely that you have a commit `E` after `D` that's the opposite of `A`. You can revert to a specific commit ID, so you can do `git checkout master; git reset --hard A; git checkout newbranch; git reset --hard D` and things will look like above. Also, have a look at [tig](http://jonas.nitro.dk/tig/) to help you visualise your git topology. – cmbuckley Aug 26 '15 at 15:20
  • thanks a ton! It explains a lot. Also the mainline branch is a shared one, like our team pulls/pushes code from mainline, so resetting it "hard", would be a problem for others? Will they have to resynchronize or something?? – user2696258 Aug 26 '15 at 15:39
  • If you've already pushed the bad commits to master on a remote server, then you're looking at further problems. It's considered bad practice to do a `push --force` which is what you need to do here, but if you can accept that everyone will need to delete their local master branches and checkout the new one after you've pushed it and they've done a `git fetch`, it might be ok. – cmbuckley Aug 26 '15 at 15:43
  • no no I havent pushed the bad commits. So is it like these bad commits on "mainline" branch are local to my system, i.e. other members in team cannot see it? – user2696258 Aug 26 '15 at 15:50
  • Yes, it's not like Subversion; when you commit, it only commits locally. It won't end up on a remote server until you've set one up and pushed your branch(es). – cmbuckley Aug 26 '15 at 15:52