我开始在我的主分支上工作,以为我的任务会很简单。过了一段时间,我意识到这需要更多的工作,我想在一个新的分公司做所有这些工作。
我如何创建一个新的分支,并采取所有这些变化与我没有脏主人?
我开始在我的主分支上工作,以为我的任务会很简单。过了一段时间,我意识到这需要更多的工作,我想在一个新的分公司做所有这些工作。
我如何创建一个新的分支,并采取所有这些变化与我没有脏主人?
当前回答
我发现这里的大多数答案都过时了。没有必要使用新的switch命令进行隐藏和弹出操作。
git switch -c new_branch -m
将创建一个名为“new_branch”的新分支,切换到它,并将所有未提交的更改作为修改文件带来。然后,您可以继续进行更改或将它们提交给新的分支等等。
其他回答
向新分支添加新更改并推送到远程:
git branch branch/name
git checkout branch/name
git push origin branch/name
我经常忘记添加原始部分来推送,并感到困惑,为什么我在bitbucket中看不到新的分支/提交
遵循以下步骤:
创建一个新分支: Git分支新增特性 签出新分支:(这不会重置你的工作) Git签出新特性 现在在这个新分支上投入你的工作: Git提交-s
使用上述步骤将使您原来的分支保持干净 你不需要做任何'git reset -hard'。
P.S. -s参数用于——signoff
就像在这个问题中所说的:Git:从master: stash上的未分期/未提交的更改创建一个分支是不必要的。
只使用:
Git checkout -b feature/newbranch
任何未完成的工作都将移交给新的部门。
如果您尝试推送,您将收到以下消息
致命:当前分支特性/新分支没有上游分支。来 推送当前分支,并将远程设置为上游,使用 Git push——set-upstream origin feature/newbranch
只需按照建议远程创建分支:
Git push——set-upstream origin feature/newbranch
因为你还没有提交任何文件,你可以将所有的更改保存到存储中,创建并切换到一个新的分支,然后将这些更改弹出到你的工作树中:
git stash # save local modifications to new stash
git checkout -b topic/newbranch
git stash pop # apply stash and remove it from the stash list
如果你还没有做出任何提交,只有(1:branch)和(3:checkout)就足够了。 或者,在一个命令中:git checkout -b newBranch
在Git 2.23+(2019年Q3)中,新的命令Git开关将在一行中创建分支(具有相同的重置类型——很难,所以要注意它的影响):
# First, save your work in progress!
git stash
# Then, one command to create *and* switch to a new branch
git switch -f -c topic/wip HEAD~3
或者,正如Alia的回答所建议的那样,使用git switch -m,不带git stash:
git switch -c topic/wip -m
--merge If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch. When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).
正如git重置手册中提到的:
$ git stash # (0) Save your work in progress
$ git branch topic/wip # (1)
$ git reset --hard HEAD~3 # (2) NOTE: use $git reset --soft HEAD~3 (explanation below)
$ git checkout topic/wip # (3)
您已经提交了一些文件,但是意识到它们还不适合放在“主”分支中。你想在topic分支中继续打磨它们,所以在当前HEAD的基础上创建“topic/wip”分支。 倒回主分支以摆脱这三个提交。 切换到“topic/wip”分支并继续工作。
再次:新方法(自2019年和Git2.23以来)在一个命令中完成所有这些:
git switch -f -c topic/wip HEAD~3
注意:由于git reset——hard命令的“破坏性”效果(它确实会重置索引和工作树。对工作树中跟踪文件的任何更改,因为<commit>被丢弃),我宁愿使用:
$ git reset --soft HEAD~3 # (2)
这将确保我不会丢失任何私有文件(未添加到索引中)。 ——soft选项根本不会触及索引文件或工作树(而是将头部重置为<commit>,就像所有模式一样)。