开发者

git going back couple of commits on a branch

I am very new to git, I checked couple of similiar questions, but, the ones I saw was slightly different, and I didn't get a strong grasp about them, so I am posting my own question.

I am sole developer on my project. I like to experiment frequently with my code, so I thought using a version control system would help. I have master branch, and a branch with a new feature I am working on. After branching out, I made some commits on feature branch that I would like to keep, and couple others I would like to discard. In the meantime, master was intact if that is relevant. I would like to find a way to learn how many commits should I go back on feature branch and also find a way to go to that commit. I ca开发者_运维百科n understand it by looking at commit messages but, I don't know where to see them.


Since you are working alone, easiest way ( and also keep your history clean) would be:

git reset --hard <commit hash of the commit before the commits you want to discard>

To get the hash use git log ( or git log --pretty=oneline) If you don't want to use hash, but know that it is four commits from HEAD, you use notations like HEAD~4

Or do git rebase -i <hash as above> and remove the lines representing the commits you want removed and do the rebase.

If you want to keep history ( and also if you were working with others, pushed to remote etc.) git revert is the safer solution.


You have a couple of options. I'd recommend a git revert if you want to discard the changes. Revert is sort of like undoing the work; it basically does a commit that undoes an existing commit. The nice part about this is, if you change your mind again later on, you can revert a revert!

If you want to revert more than one commit, which it sounds like you want to, the syntax looks something like this:

git revert branchname~5..branchname~3

This will revert the 5th last commit up to the 3rd last commit. git revert will immediately commit the revert. If you want to see the changes it will make before committing; then you can specify --no-commit, then commit it yourself later using git commit.

You can also revert individual commits like so:

git revert branchname~5

If you want to see all of the commits you've made so far, you can use git log.

And finally, using git log you can see the commit hash. git-revert can also accept a hash so you don't have to count how many revisions back it is.

git revert sha1hashhere


If you are ok with deleting the unwanted commits

  1. Find the common parent the two have. You can skip this step if you know far far back you want to go.
    git merge-base featureBranch masterBranch
  2. Checkout your featureBranch
    git checkout featureBranch
  3. Rebase that branch, removing the commits you don't want (delete the lines that contain your unwanted commits when your editor pops up.
    git rebase -i sha_from_step_1 or you can use git rebase -i HEAD@{n} where n is how far back you want to go.
  4. Merge into master
    git merge master


one comit back = HEAD^

two commit back = HEAD^^

read mapage "man gitrevisions"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜