回到SourceTree中以前的提交

7

我刚接触Git,试图在SourceTree中恢复到以前的提交记录。 我右键单击了要还原的提交记录,然后点击checkout。 弹出提示说我的工作副本将成为一个分离的头。 这是什么意思,我应该避免这种情况吗?


1
可能是重复的问题,参考如何使用SourceTree将代码回滚到早期提交版本 - Tim Biegeleisen
1
你可以撤销这个提交,但更安全的做法是创建一个撤销提交来撤销此提交。 - Tim Biegeleisen
3个回答

8
根据Git-Tower文章:Git中的“detached HEAD”是什么?

Understanding how "checkout" works

With the "git checkout" command, you determine which revision of your project you want to work on. Git then places all of that revision's files in your working copy folder.

Normally, you use a branch name to communicate with "git checkout"

$ git checkout development

However, you can also provide the SHA1 hash of a specific commit instead:

$ git checkout 56a4e5c08
Note: checking out '56a4e5c08'.

You are in 'detached HEAD' state...

This exact state - when a specific commit is checked out instead of a branch - is what's called a detached HEAD.

enter image description here

The problem with a detached HEAD

The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when checking out a proper branch name, Git automatically moves the HEAD pointer along when you create a new commit. You are automatically on the newest commit of the chosen branch.

When you instead choose to check out a commit hash, Git won't do this for you. The consequence is that when you make changes and commit them, these changes do NOT belong to any branch. This means they can easily get lost once you check out a different revision or branch: not being recorded in the context of a branch, you lack the possibility to access that state easily (unless you have a brilliant memory and can remember the commit hash of that new commit...).

摘要: 在使用SourceTree时,请切换到特定的分支而不是特定的提交。


2

2020年12月9日。

我正在使用 Sourcetree。如果您想要回到之前的提交,只需双击之前的提交即可。之后,如果您想要,可以创建一个新分支并将其与旧分支合并。

enter image description here


0

这个问题似乎与git不完全相关,而是与您使用的git客户端/提供者有关(我怀疑是bitbucket)。

我建议您使用命令行客户端而不是Web UI来更好地学习git。

在分离头状态下,您所做的任何更改(和提交)都会与提交树分离,并且您需要额外的工作将该提交放回提交树中。通常我们不会在分离头状态下进行更改,它用于重新排列提交树。但是在分离状态下进行实验是值得的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接