Question

i'm using git to manage my project, but i'm not much experienced in this kind of programs, so often i use them improperly(eg: i commit many (unrelated) changes in only one commit). I'm trying to using them better.

Btw i noticed that i started to develop an extension of my project that would be better fitting into a branch. So from the git gui i created a new branch that's a copy of the master. Now i want to revert the master to the last commit i made that it is not relevant to the project. I know what this commit is, but i don't know how to revert all the commits from HEAD to this one, and i don't know if this will influence the branch(it would be a real pain to lose all the new work..)

So, how can i do?

Ps: usually i use git gui, but i have no problem in using git bash. I use win7 and i'm managing an java android project in eclipse

Was it helpful?

Solution

If you have your branch created, change back to the master and reset it to the desired commit with git reset --hard commitid. Now 'master' is set to this commit and branch still points to the commit you had it configured for previously.

Branch names are references or alternate names for specific commits. reset allows you to change the commit that your reference points at and force the working tree to match (with --hard)

OTHER TIPS

Changes to the master branch will not affect the history for your new branch.

If you want to change master's HEAD to be at a specific commit, use reset:

git checkout master
git reset --hard <commit id>

If your sequence of events were:

  1. Commit big change.
  2. Create new branch.

And you just want to get master back to before the previous commit, use reset:

git checkout master
git reset --hard HEAD^

If your sequence of events were:

  1. Commit big change.
  2. Commit other changes (which should stay in master).
  3. Create new branch.

And you just want to remove the big change commit from master while leaving all other commits intact, use revert:

git checkout master
git revert <commit id>

If your sequence of events were:

  1. Commit big change.
  2. Commit other changes (which should also be removed from master).
  3. Create new branch.

And you want to remove all recent commits up through and including big change from master, use reset:

git checkout master
git reset --hard <big change commit id>^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top