我开始研究一个新特性,在编码了一段时间后,我决定这个特性应该属于自己的分支。

如何将现有未提交的更改移动到新分支并重置当前分支?

我想重置当前分支,同时保留新功能的现有工作。


当前回答

我使用@Robin回答并列出我所做的一切,

git status                               <-- review/list uncommitted changes
git stash                                <-- stash uncommitted changes
git stash branch <new-branch> stash@{1}  <-- create a branch from stash
git add .                                <-- add local changes
git status                               <-- review the status; ready to commit
git commit -m "local changes ..."        <-- commit the changes
git branch --list                        <-- see list of branches incl the one created above
git status                               <-- nothing to commit, working tree (new-branch) is clean
git checkout <old-branch>                <-- switch back

! 如果回购有多个存储,请查看要应用于新分支机构的存储:

git stash list  
  stash@{0}: WIP on ...  
  stash@{1}: WIP on ...

并通过以下方式检查各个藏匿点,

git stash show stash@{1}

或者立即检查所有储藏物:

git stash list -p

其他回答

这可能有助于所有使用GIT工具的人

命令

切换分支-它会将您的更改移动到新分支。然后您可以提交更改。

 $ git checkout -b <new-branch>

乌龟GIT

右键单击存储库,然后使用TortoiseGit->切换/签出

源代码树

使用“签出”按钮切换分支。单击分支后,您将看到顶部的“结帐”按钮。来自当前分支的更改将自动应用。然后你就可以提交它们了。

我使用@Robin回答并列出我所做的一切,

git status                               <-- review/list uncommitted changes
git stash                                <-- stash uncommitted changes
git stash branch <new-branch> stash@{1}  <-- create a branch from stash
git add .                                <-- add local changes
git status                               <-- review the status; ready to commit
git commit -m "local changes ..."        <-- commit the changes
git branch --list                        <-- see list of branches incl the one created above
git status                               <-- nothing to commit, working tree (new-branch) is clean
git checkout <old-branch>                <-- switch back

! 如果回购有多个存储,请查看要应用于新分支机构的存储:

git stash list  
  stash@{0}: WIP on ...  
  stash@{1}: WIP on ...

并通过以下方式检查各个藏匿点,

git stash show stash@{1}

或者立即检查所有储藏物:

git stash list -p

更新2020/Git 2.23

Git 2.23添加了新的switch子命令,试图消除由于超负荷使用checkout(切换分支、恢复文件、分离HEAD等)而产生的一些混乱

从这个版本的Git开始,将checkout命令替换为:

git switch -c <new-branch>

行为相同且保持不变。


更新2020/Git 2.23之前

使用以下方法:

git checkout -b <new-branch>

这将使当前分支保持原样,创建并签出新分支并保留所有更改。然后,您可以在文件中暂存要提交的更改:

git add <files>

并向您的新分支机构承诺:

git commit -m "<Brief description of this commit>"

工作目录中的更改和索引中暂存的更改尚不属于任何分支。这将更改这些修改将结束的分支。

您不重置原始分支,它保持原样。<old branch>上的最后一次提交仍将保持不变。因此,您签出-b,然后提交。

您还可以创建bash别名来完成所有这些操作。

这将创建新命令。。。

gitco<branch>-检查指定的分支,并随身携带当前的更改gitconew<new branch name>-使用指定的名称(从master分支)创建一个新的分支,并将当前的更改记录在其中

以下是设置别名的步骤。。。

将以下内容添加到~/.bash_profile

gitco ()
{
  git stash && git checkout $1 && git stash apply
}

gitconew ()
{
  git stash && git checkout master && git checkout -b $1 && git stash apply
}

运行source~/.bash_profile以重新加载配置文件

现在您可以运行gitco和gitconew别名命令了。

以下是有关bash别名的更多信息->https://linuxize.com/post/how-to-create-bash-aliases/

或者:

将当前更改保存到临时存储:$git存储基于此存储创建新分支,然后切换到新分支:$git stash branch<new branch>stash@{0}

提示:使用tab键可以减少键入存储名称。