嘗試從 gitab gui 創建一個分支并使用以下命令
>>git pull origin remote_branch_name
>>git checkout remote_branch_name
沒有用 -> 當我使用命令 git branch 時沒有得到遠程分支名稱。還想知道如何創建遠程分支以及使用命令列從遠程分支創建本地分支的整個程序嗎?
uj5u.com熱心網友回復:
分支創建(遠程 vs 本地)
OP:如何或者更確切地說是何時從 Git 中的遠程分支創建本地分支?
本地分支在以下情況下被創建:
- 在您的本地存盤庫中簽出(切換到)遠程分支
- 在本地創建一個新分支(遠程上不存在)
在上面的例子中,一個遠程存盤庫已經被克隆,整個存盤庫包括唯一可用的分支(master)已經通過本地鏡像o/master;它還master自動創建了一個本地分支來跟蹤它的遠程計數器部分。每當克隆新存盤庫時,默認分支都會自動發生這種情況。
還要注意super-cool-feature當前在本地簽出但遠程尚不存在的分支。要遠程發布分支,必須首先推送它。
切換到現有的遠程分支
$ git switch branch_name
創建一個新的本地分支(并自動切換到它)
$ git switch --create=branch_name
遠程發布本地分支
$ git push --set-upstream origin branch_name
來源:上面的例子是從這個博客借來的。
uj5u.com熱心網友回復:
以下是要采取的步驟:
找出您擁有的 Git 版本。運行
git --version。如果它比 Git 2.0 舊,請盡可能更新它。理想情況下,它應該在 2.17 左右(以避免各種錯誤;總的來說,更新的更好,盡管例如git stash最近引入的一些新錯誤仍在被壓縮,因此擁有最新的Git并不總是更好版本)。通過運行以下命令找出您當前的存盤庫是否是單分支存盤庫:
git config --get remote.origin.fetch(注意:這假設您的遙控器名為
origin。這是標準名稱;它是您在git pull origin remote_branch_name命令中顯示的名稱。)這應該列印:refs/heads/*:refs/remotes/origin/*如果它列印出類似 的內容
refs/heads/master:refs/remotes/origin/master,則您有一個單分支克隆(在這種情況下,單個分支為master)。請參閱如何“撤消” --single-branch 克隆?或運行:git remote set-branches origin "*"要解決這個問題。(如果它列印出預期的正常內容,則您無需在此處執行任何操作。)
跑:
git fetch --prune origin不要跑
git pull。不要輸入分支名稱。只需運行git fetch或git fetch origin,最好使用--prune(盡管修剪部分是可選的)。origin這里的顯式表示從 中獲取origin。如果沒有名稱,Git 會確定要使用哪個遠程,如果您有多個遠程,可以從其他遠程而不是 from 獲取origin。您幾乎可以肯定只有一個名為 named 的遙控器,origin因此對于使用哪個遙控器毫無疑問。使用git remote -v列出一組遙控器,你有; 它將為每個遙控器列印一對行。運行
git branch -r列出遠程跟蹤名字是git fetch origin --prune留下。這些源自您的 Git 可以自動創建的分支名稱(如果您還沒有的話)。例如,在 Git 的 Git 存盤庫的 Git 克隆中,我得到:$ git branch -r origin/HEAD -> origin/master origin/main origin/maint origin/master origin/next origin/seen origin/todo這意味著我可以
todo輕松創建一個名為的分支。要從這些遠程跟蹤名稱之一創建新的本地分支,請運行:
git switch <name>(填寫
name部分,去掉尖括號<>,帶有遠程跟蹤名稱,去掉origin/部分)。如果您的 Git 早于 2.23,請使用:git checkout <name>以獲得相同的效果。這會告訴您的 Git 使用猜測模式,Git 以前將這種模式稱為DWIM 模式:如果您還沒有一個具有該名稱的分支,Git 會猜測您打算讓它基于遠程跟蹤創建一個新的本地分支名稱。
如果由于某種原因該--guess模式不起作用——例如,如果你在自己的 Git 配置中禁用了它,你可以在一些最近的 Git 版本中這樣做——你總是可以更加明確。例如,如果有一個origin/feature/short并且您希望feature/short基于來創建origin/feature/short,請運行:
git switch --track origin/feature/short
你的 Git 知道這origin/feature/short是一個遠程跟蹤名稱(因為它被發現是一個,被git fetch origin之前留下了),這--track意味著:通過洗掉origin/部分將此遠程跟蹤名稱轉換為分支名稱,然后創建該分支名稱,指向到正確的提交。 這與--guess選項(默認為“on”)所做的技巧相同,除了不是在說“沒有這樣的分支”之前的最后一分鐘執行此操作,Git 會盡早執行并且不必猜猜。
還想知道如何創建遠程分支...
In a sense, you can't do this. Imagine you have a brother or friend named Fred. You tell Fred: Change your shirt! The one you have on has a big hole in it! If Fred changes his shirt, did you make him do it? If your answer is "yes, I made him do that", then you can create a remote branch. If it's "no, he chose to do that, I just asked him first", then you can't create a remote branch.
A remote is some other Git repository. Each remote you connect to from your Git repository has a name. The standard name for the first remote is origin (just like the standard name for your brother is Fred ??). You don't really create a remote branch. You just ask or tell some remote, such as origin, to create a branch. He—we'll assume here that origin is a guy, or at least goes by masculine pronouns—either does what you ask, or doesn't.
So the more-precise question is: How do I ask a remote repository to create a branch name? The answer is: You run git push.
When you run:
git push origin my-branch-name:his-branch-name
or:
git push origin one-branch-name
you are instructing your Git software to call up some other Git repository via your name origin. That other Git repository, which will be updated (or not) by some other Git software, exists independently of your Git repository. He has his own branch names, each one of which stores a commit hash ID. He stores commits. Your Git stores commits and finds them with your branch names. You now have your Git send to his Git any commits that you have, that he does not, that he will need, and then your git push ends with a polite request to him: Please, if it's OK, create or update your branch name ________ (fill in the blank) to point to commit hash ID ________ (fill in the blank).
Your Git fills in the first blank with the branch name on his side, from the my-branch-name:his-branch-name pair. If you used the git push origin one-branch-name syntax, your Git fills in the first blank with one-branch-name. That is, you and he will use the same branch name.
Your Git fills in the second blank—the commit hash ID—with the hash ID from your branch name, i.e., from the my-branch-name part of the pair. If you used the one-branch-name syntax, your Git fills in the second blank with the hash ID from your name one-branch-name.
Either way, you've now asked him—origin—to create or update a branch name in his repository. He will either obey, or not. You have no direct control at this point in time as to whether he will obey. (If you "own" the other Git repository—for instance, if it's on GitHub but you're the owner of the GitHub repository—you can, at some earlier time, log in to GitHub on the Web and set up your own permissions to determine whether your later git push will be obeyed. But that's separate from your git push operation.)
If this branch name is new to origin, your request is one to create a branch name. If he obeys, you have created, or at least caused the creation of, the corresponding branch name in the other Git repository. Whether you want to call that "creating a remote branch" is up to you, but in a technical sense, what you really did was send a request. He—the Git and repository at origin—decided whether to obey the request. So it always takes at least a little bit of co?peration.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389533.html
上一篇:在遠程壓縮后提取舊的提交ID
