我如何从我目前的状态转向在某个承诺上拍摄的截图?

如果我做 git 登录,那么我会得到以下输出:

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <me@me.com>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

我如何转向11月3日的承诺,即承诺0d1d7fc?


当前回答

如果你想暂时转换变更,因为

有人承诺的代码正在破坏构建或破坏你正在工作的功能

您可以使用 git log 搜索最后一个工作承诺,然后运行:

git rebase --onto <commitId>

当远程分支重新运行时,您可以

git pull --rebase

这种方法比临时变更的Git支票更好,因为你不处于分离状态。

其他回答

首先,什么是头?

HEAD 仅仅是指当前的承诺(后者)在当前的分支上,在任何时间(不包括 git 工作)只能有一个 HEAD。


脱头头

如果你不是最近的承诺 - 意思是,头指的是历史上以前的承诺,它被称为分开的头。

此分類上一篇

此分類上一篇



去支票

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

在此点上,您可以创建一个分支,并从此开始工作:

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git reflog 将显示任何更改,更新了 HEAD 并检查所需的 reflog 输入将设置 HEAD 返回此承诺。

git reflog
git checkout HEAD@{...}


“移动”你的头回到所需的承诺。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

注意: (由于 Git 2.7) 你也可以使用 git rebase --no-autostash 也。


此分類上一篇

这是另一种方式直接重定向到最近的承诺。

git stash
git stash clear

它直接清除你自最后的承诺以来所做的所有变化。

PS:它有点问题;它还会删除你最近存储的所有存储变更,我猜在大多数情况下不重要。

我发现最不复杂的方式,把一个分支转向任何特定的承诺,在那里你不能改变历史,就是:

查看承诺或分支您的愿望转向. 编辑.git/HEAD 并更改转向您转向的分支。

如如:

echo 'ref: refs/heads/example' > .git/HEAD

如果你做 git 状态,你应该看到你所在的分支和你想转向的分支之间的所有变化。

如果一切都看起来很好,你可以承诺. 你也可以使用 git diff revert..example 确保它是相同的。

在GitKraken,你可以这样做:

右键单击您要重新设置的承诺,选择: 重新设置此承诺/硬: 右键单击承诺再次,选择: 当前分支名称/ 按: 单击强度按:

注意:你需要小心,因为硬重新设置后所有的承诺历史都丢失了,这种行动是不可逆转的。

您可以自行完成所有这些初步步骤,然后按一下返回 Git 存储库。

使用 git pull --all 命令从 Bitbucket 播放您的存储库的最新版本. 从您的终端运行 Git 存储库命令 -n 4 之后的号码决定从您本地历史上最新的存储库中的订单数量. $ git 存储库 -n 4 使用 git reset --hard HEAD~N 重新设置存储库历史的头,在那里 N 是订单数量

如果您想要 Git 存储库到以前的承诺:-

git pull --all
git reset --hard HEAD~1
git push --force