如何在 git 中将 master 移动到当前 head? git checkoutgit reset --hard <commit_id>

问题描述

目前我的头部和主人如下-

2441dc3 (HEAD) Made backend route for student attendance
2b27490 Made storage to localstorage from the classname
577bd81 (origin/master,master) Made attendace UI working though there are some errors

我现在如何让我现在的头成为主人?

解决方法

您在当前的 master 处重新创建 HEAD,然后检查它:

git branch -f master
git checkout master

给你

2441dc3 (HEAD -> master) Made backend route for student attendance
2b27490 Made storage to localstorage from the classname
577bd81 (origin/master) Made attendace UI working though there are some errors
,

您可以对 master 分支进行硬重置,或者重置为 master 的最新 SHA-1 哈希:

# from your current branch
git reset --hard origin/master
git reset --hard 577bd81
,

HEAD?

HEAD 只是对当前分支上当前提交(最新)的引用。
在任何给定时间只能有一个 HEAD(不包括 git worktree)。

HEAD 的内容存储在 .git/HEAD 中,它包含当前提交的 40 字节 SHA-1。


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to 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 reset --hard <commit_id>

“移动”你的 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 that were
# changed since the commit you reset to.
  • 注意:([自 Git 2.7][8])您也可以使用 git rebase --no-autostash

Enter image description here