Git checkout - 切换回 HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42082338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:46:49  来源:igfitidea点击:

Git checkout - switching back to HEAD

gitgit-checkout

提问by asdfgh

I've been doing my project while at some point I discovered that one thing stopped working. I needed to look up the state of my code when it was working correctly, so I've decided to use git checkout (because I wanted to check-something-out). And so I've done

我一直在做我的项目,而在某个时候我发现一件事停止了工作。我需要在代码正常工作时查看它的状态,所以我决定使用 git checkout(因为我想检查一些东西)。所以我已经完成了

git checkout SHA

couple times while going back to point from which I can't go to HEAD, the output is following:

几次回到我无法转到 HEAD 的点时,输出如下:

git checkout SHA-HEAD

error: Your local changes to the following files would be overwritten by checkout:
    [list of files]
Please, commit your changes or stash them before you can switch branches.
Aborting

I am pretty much sure I have NOT changed anything. The command

我很确定我没有改变任何东西。命令

git checkout master

gives the same output.

给出相同的输出。

Is there a way to go back to HEAD?

有没有办法回到HEAD?

What is the safe way of "jumping over" history commits?

“跳过”历史提交的安全方式是什么?

回答by Sajib Khan

You can stash(save the changes in temporary box) then, back to masterbranch HEAD.

您可以stash(将更改保存在临时框中)然后返回到master分支 HEAD。

$ git add .
$ git stash
$ git checkout master


Jump Over Commits Back and Forth:

来回跳过提交:

  • Go to a specific commit-sha.

    $ git checkout <commit-sha>
    
  • If you have uncommitted changes here then, you can checkout to a new branch, Add, Commit, Push the current branch to the remote.

    # checkout a new branch, add, commit, push
    $ git checkout -b <branch-name>
    $ git add .
    $ git commit -m 'Commit message'
    $ git push origin HEAD          # push the current branch to remote 
    
    $ git checkout master           # back to master branch now
    
  • If you have changes in the specific commit and don't want to keep the changes, you can do stashor resetthen checkout to master(or, any other branch).

    # stash
    $ git add -A
    $ git stash
    $ git checkout master
    
    # reset
    $ git reset --hard HEAD
    $ git checkout master
    
  • After checking out a specific commit if you have no uncommitted change then, just back to masteror otherbranch.

    $ git status          # see the changes
    $ git checkout master
    
    # or, shortcut
    $ git checkout -      # back to the previous state
    
  • 转到特定的commit-sha.

    $ git checkout <commit-sha>
    
  • 如果您在此处有未提交的更改,则可以签出到新分支、添加、提交、将当前分支推送到远程。

    # checkout a new branch, add, commit, push
    $ git checkout -b <branch-name>
    $ git add .
    $ git commit -m 'Commit message'
    $ git push origin HEAD          # push the current branch to remote 
    
    $ git checkout master           # back to master branch now
    
  • 如果您在特定提交中有更改并且不想保留更改,您可以执行stashreset然后签出到master(或任何其他分支)。

    # stash
    $ git add -A
    $ git stash
    $ git checkout master
    
    # reset
    $ git reset --hard HEAD
    $ git checkout master
    
  • 如果您没有未提交的更改,则在检查特定提交后,只需返回masterother分支。

    $ git status          # see the changes
    $ git checkout master
    
    # or, shortcut
    $ git checkout -      # back to the previous state