我正在学习如何使用 git,我需要从主 github 存储库进行克隆,对其进行分支,进行更改,然后将分支推送到 github 存储库 - 但我发现自己对最好的方法有点困惑就是这样。我知道 git 网上有很多资源,但我找不到一个正是这个问题的资源(尽管如果我错了,我很乐意使用它)。
我一直在我自己的私人 github 存储库上进行测试以查看差异,但似乎没有任何差异。
- 从存储库克隆。
git clone [email protected]:organization/repo
- 创建一个新分支
git checkout -b newbranch
- 进行更改、添加和提交,
touch file
git add file
git commit -m "added file"
- 当我尝试推送没有上游分支时
git push
,我收到一条错误消息“致命:当前分支 newbranch 没有上游分支。
要推送当前分支并将远程设置为上游,请使用
git push --set-upstream origin newbranch
但我也看到很多在线教程使用
git push -u origin newbranch
这之间有什么功能上的区别吗?我确信网上有一些关于差异的资源,但 git 对我来说有点令人困惑,我想我会直接问这个问题来看看人们的想法。
I'm learning how to use git and I need to clone from a master github repo, branch it, make changes, and push the branch to the github repo - but I'm finding myself a bit confused on what the best way to do that would be. I know there are so many resources on git online but I can't find one that is this exact problem (although happy to use that if I'm wrong).
I've been testing on my own private github repo to see the differences and there doesn't seem to be any.
- clone from repo.
git clone [email protected]:organization/repo
- create a new branch
git checkout -b newbranch
- make changes, add, and commit
touch file
git add file
git commit -m "added file"
- when I try to push there is no upstream
git push
I get an error saying "fatal: The current branch newbranch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin newbranch
but I also see a lot of online tutorials using
git push -u origin newbranch
Is there any functional difference between this? I'm sure there is some resource online about the difference, but git is a bit confusing to me and I though i'd ask this one direct question to see what people thought.
发布评论
评论(2)
它们是完全一样的东西。 Unix 命令的许多选项都有短形式和长形式。您可以通过运行 git help push 来亲自查看这一点。
They're exactly the same thing. Lots of options to unix commands have both a short and a long form. You can see this for yourself by running
git help push
.当您设置上游(或跟踪)分支时,您可以简单地执行拉取和推送,而无需使用
-u
而不是--set-upstream
指定目标分支。 Git 自动知道它必须将新提交获取到远程跟踪分支。这是一篇关于它的文章 此处
When you set your upstream (or tracking) branches, you can simply execute pulls and pushes without having to specify the target branch using
-u
instead of--set-upstream
. Git automatically knows that it has to fetch the new commits to the remote tracking branch.Here's an article about it here