我使用 git 提交了一些更改,现在想将它们放在一个新的分支中。在几个地方解释了如何实现这一点,例如 here
我的困惑更多地与此任务的本地/远程方面有关。
我首先将 github 上的存储库 fork 到 myForkOnGitHubRepo。 然后我将该存储库克隆到我的本地 PC:
local: git clone myForkOnGitHubRepo
然后我更改了源,提交并推送:
local: git add .
local: git commit
local: git push # (actually I used the minGW Gui for that one since the command line push issued a warning)
现在我的本地更改在 myForkOnGitHubRepo 中可见。美好的。 现在我注意到我更愿意使用一个新分支(并且在 myForkOnGitHubRepo 的那个新分支中也有更改)。
我可以按照我在开头发布的链接中的说明进行操作是否正确,即
local: git branch newbranch
local: git reset --hard HEAD~1 # only one commit was done
然后——嗯,现在怎么办?我只需要再次 push 吗?或者我是否需要显式推送新分支? (对不起,如果这是非常基本的,我以前从未使用过)。
在创建新分支之前我还需要做些什么吗?我的理解是,推送之后,本地和远程的状态是一样的,对吗?
最佳答案
local: git branch newbranch
local: git reset --hard HEAD~1 # only one commit was done
现在,随着 git 历史的改变,强制(-f)推送到 remote/master
。
$ git push -f origin master
checkout 到 newbranch
并推送 newbranch
。
$ git checkout newbranch
$ git push origin newbranch
常见的情况是
master
分支应该保持工作或者不被破坏。因此,当需要处理新功能时,从master
创建一个新分支(例如,feature
)。$ git checkout master $ git checkout -b feature
在
feature
分支上工作。完成feature
分支add
后,commit
和push
到远程。$ git add. $ git commit -m 'message' $ git push origin feature
现在,如果
feature
分支的所有测试都正常,那么创建 Pull request 或与 master merge$ git checkout master $ git pull origin feature # pull = fetch + merge $ git push origin master # update remote/master
My understanding is that, after the push, local and remote are in the same state, is that correct?
是的,当你推送到远程时,你的本地和远程是同步的(相同的数据)
现在,如果您需要在另一个分支 (例如,dev
) 然后,只需从 master
创建一个分支 (dev
) 并将 feature
分支 pull 入 开发
$ git checkout master
$ git checkout -b dev
$ git pull origin feature # pull 'feature' into 'dev'
# do changes here
$ git commit -am 'Added new feature' # add & commit
$ git push origin dev
关于git - 移动 git commit 到新分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42208494/