什么时候使用 git push 的某些参数?

When to use certain arguments with git push?

我见过三个版本的推送命令:

git push -u remote_repo_ref local_branch_name

git push remote_repo_ref local_branch_name

git push

我不太清楚什么时候使用哪个。通常 remote_repo_reforiginlocal_branch_namemaster 但我在这里使用一般标签以使我的问题更笼统。

第一次推送到 remote/upstream 时使用 git push -u。这是一个示例 "below",说明您何时需要使用 git push -u remote_repo_ref local_branch_name

比方说,我们有一些代码已经签入了,我们只需要添加一个新分支并将其签入......

=>

# view current branches. 
za:webapp za$ git branch
  master
* paperclip_file_up_down_load_and_s3

 =>
# create a new branch called some _feature
za:webapp za$ git checkout -b some_feature
M   app/models/video.rb
Switched to a new branch 'some_feature' paperclip_file_up_down_load_and_s3

 =>
# Check what is under .git/refs/remotes/origin/
# you can get more details suing za$ git remote show origin 
# Note: branch soe_feature is not there yet
za:webapp za$ ls -lad .git/refs/remotes/origin/*
-rw-r--r--  1 za  staff  41 Nov 11 13:49 .git/refs/remotes/origin/master
-rw-r--r--  1 za  staff  41 Nov 26 14:06 .git/refs/remotes/origin/paperclip_file_up_down_load_and_s3


#Add it using  git push -u origin some_feature
za:webapp za$ git push -u origin some_feature
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/codepedia/webapp.git
 * [new branch]      some_feature -> some_feature
Branch some_feature set up to track remote branch some_feature from origin.

=>
# Check again, it is there. Was linked remote origin via the flag -u
# You can also run  git push -u origin some_feature
za:webapp za$ ls -lad .git/refs/remotes/origin/*
-rw-r--r--  1 za  staff  41 Nov 11 13:49 .git/refs/remotes/origin/master
-rw-r--r--  1 za  staff  41 Nov 26 14:06 .git/refs/remotes/origin/paperclip_file_up_down_load_and_s3
-rw-r--r--  1 za  staff  41 Jan 21 21:09 .git/refs/remotes/origin/some_feature

至于另外两个:

git pushgit push remote_repo_ref local_branch_name

的 shorthand 版本

git push remote_repo_ref local_branch_name 你只是 verbose/explicit 在这里。当本地 master 或分支已经签入并链接到上游时,您使用 git push

希望对您有所帮助!!