0

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.

user51819
  • 315
  • 5
  • 11
  • 1
    When using GitHub, you’ll almost never want to use the middle form, which is the same as the first one but doesn’t set the local branch’s upstream. Every local branch can have an *upstream*, which is the remote branch that `git push` (the last form!) will automatically push to when you’re on that local branch. When you’ve newly created a branch, though, that upstream won’t yet be set, and that’s what `-u` is for. – Ry- Jan 22 '18 at 02:04
  • 1
    Did you try reading the man page? It's very clear on what it means to omit the remote name and the local ref, and on what `-u` means. – matt Jan 22 '18 at 02:04
  • 2
    @matt It says "set upstream tracking reference" but I don't know what this really means or when we'd want/need to do it, etc. I don't know if I'd say it's "very clear" – user51819 Jan 22 '18 at 02:06
  • @Ryan So `-u` is like saying "next time you do `git push` when you have `local_branch_name` checked out it will automatically push to the same branch on `remote_repo_ref`? – user51819 Jan 22 '18 at 02:10
  • 1
    Yep! It’s also the branch `git pull` will pull from, and checking out a branch that only exists on a remote (rather than creating a new one locally) will automatically set the upstream too. You can see your upstreams with `git branch -vv` (they’re in square brackets). – Ry- Jan 22 '18 at 02:11
  • @Ryan Is there a term we use for labeling such branches? i.e. a branch without an upstream is a (blank) branch? – user51819 Jan 22 '18 at 02:13

1 Answers1

1

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!!

z atef
  • 7,138
  • 3
  • 55
  • 50
  • If you keep using feature branches the upstream will be constantly changing so in that case it wouldn't matter if you use -u, am I right? – darkace Mar 04 '22 at 13:22