0

我在我的主分支本地进行了一些更改并提交,但没有推送。现在,我改变了主意,我想创建一个新的测试分支,接受所有要推送的更改,将它们提交到新分支并从主分支中删除。

所以(不是真正的输出,是手写的):

# get the code
git clone ...

# I am in the main branch
git branch
* main

# ...do some changes...
# and commit them
git commit --all

# I am ahead by 1 commit
git status
Your branch is ahead of 'origin/main' by 1 commit

# I changed my mind, I don't want to
# git push
# I want to move those changes to a new branch

git checkout -b test

# WHAT NOW?
# - move the changes that are "ahead" to the "test" branch
# - restore the main branch to the state before the commits
4

1 回答 1

5

“提前”的变化只是介于origin/main和之间的变化main。因此,要重置main为旧状态,您只需git reset --hard origin/main. WHAT NOW 的完整设置将是:

git checkout main
git reset --hard origin/main

……就是这样。请注意,“现在做什么”的第一步已经由 完成git checkout -b test,所以剩下的唯一步骤就是恢复mainorigin/main

注意:这假设更改已提交,如问题中所述。在结帐时运行git reset --hard未提交的更改将使您丢失所做的更改。如果您不确定更改是否已提交,请运行git status;成功提交后,它会告诉你有nothing to commit.

于 2013-10-21T11:46:09.327 回答