Search code examples
gitgithubgit-push

When to use certain arguments with git push?


I've seen three versions of the push command:

git push -u remote_repo_ref local_branch_name

and

git push remote_repo_ref local_branch_name

and

git push

I'm a little unclear when to use which one. Usually remote_repo_ref is origin and local_branch_name is master but I'm using general labels here to make my question more generalized.


Solution

  • Use git push -u when you are pushing to the remote/upstream for the first time. Here is an example "below"of when you will you need to be using git push -u remote_repo_ref local_branch_name .

    Lets say, we have some code that is checked in already and we just need to add a new branch and check it in.....

    =>
    
    # 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
    

    As for the other two:

    git push is the shorthand version for git push remote_repo_ref local_branch_name

    git push remote_repo_ref local_branch_name You just being verbose/explicit here. You use git push when the local master OR branch is already checked in and is linked to the upstream.

    Hope that helps!!