4

我正在使用 Git。我不小心开始在 Master 上进行所有代码更改。如何创建一个分支“new_feature”并将我的所有更改移至“new_feature”分支并移出主分支?

4

4 回答 4

3

如果您已经提交了代码,则只需运行git checkout -b new_feature. 这将创建您的新分支并将您切换到该新分支。

如果您尚未提交更改,请运行git checkout -b new_feature然后提交更改。

如果在创建新分支并提交代码后,您需要恢复主分支:

git checkout master              # switch to the master branch
git reset --hard origin/master   # revert master to the current origin/master

请注意,这git reset --hard将导致您丢失任何未提交的更改。

于 2013-03-13T05:43:35.903 回答
2

如果你还没有提交到 master 分支。做一个git diff来创建一个补丁。创建新分支

git checkout -b new_branch

并使用git apply

您可以关注此问题,以更好地了解如何从未提交/未暂存的更改创建补丁

于 2013-03-13T05:43:16.013 回答
2

如果您尚未提交更改,只需切换到新分支:

git checkout -b new_features

这会将您的所有更改带到新分支而不影响主分支。在这里,您可以提交所有更改。

于 2017-10-11T05:16:05.233 回答
1

试试这个:

% git commit ...               # save any untracked changes
% git branch <new-branch>      # created a branch, but you're still on master
% git log                      # find the first development commit
% git reset --hard "<commit>^" # set the master branch to before the commit
% git checkout <new-branch>    # recommence your development work
% git checkout master          # return to work prior to development

小心reset --hard,因为您可能会丢失未跟踪的更改。此外,不要reset --hard超出已经推送到远程的提交。

于 2013-03-13T05:43:27.800 回答