簡體   English   中英

git 如何創建本地 master 分支並與遠程 master 同步

[英]git how to create local master branch and sync with remote master

我被告知“永遠不要更改 repo 分支上的主分支,或任何來自上游的分支。您的主分支將從上游分叉,您將無法將上游拉入本地分支。 ”。 這對我來說很有意義。

我把所有的提交都提交到了我的本地主分支。 所以,根據上述建議,我現在想保持我的主人干凈。 所以,我檢查了 master 到 old_master

git 分支

*master

git checkout -b patchv1

現在,我所有的更改都在patchv1分支中。

我通過運行以下命令強制刪除了主分支

git 分支 -D 主

我以為我會創建孤兒主然后 git pull

git checkout --orphan master

Switched to a new branch 'master'

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

git pull 遠程原點

fatal: 'remote' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

git遠程顯示原點

* remote origin
  Fetch URL: https://giturl/my_org_name/runbooks.git
  Push  URL: https://giturl/my_org_name/runbooks.git
  HEAD branch: master
  Remote branches:
    custom-patch-1            tracked

另外,當我執行 git branch 時,它不會顯示這個新創建的孤立主分支。 它只是列出了我在刪除之前從 master 檢出的 patchv1 分支。 而且,這似乎也不是我目前的分支。

git 分支

patchv1

如何重新創建 master 分支並確保它與遠程 master 同步?

我認為你走錯了路。 孤兒分支是一個分支,與原始主分支(無論是遠程還是本地)沒有任何關系 所以我認為這不是你想要的方式。 我最好的建議是: go back to your branch patchv1 然后與遠程主同步的最簡單方法(在您簽出patchv1 )是簡單地運行:

git pull -r origin master

這將得到遠程分支主的狀態,那么它會重訂遠程主分支的新位置的頂部您patchv1分支....那就是,當然,如果你想變基。 如果您想運行合並,請運行相同的操作,但不帶-r選項。

現在,您本地的主分支。 如果您根本不打算使用它那么刪除它就可以了。 就像你一樣,我寧願讓我的本地分支將它們的上游分支設置為遠程分支,而不是被迫先同步本地 master 然后將更改拉入功能分支......但這是一個品味問題,我思考。

您不需要創建masterorphan ,只需git checkout master然后git pull

一個典型的工作流程是在本地master上運行git pull ,以便它從remote master分支中提取最新的。 然后你創建一個功能分支: git checkout -b my-feature ,做你的工作,然后git push -u origin my-feature這樣你就可以打開一個拉取請求將功能分支合並到主分支。

為了讓你的特性分支最新的帶主,你總是可以做git merge origin/master (或git pull -r origin master ,這使新上提交的頂你的提交master )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM