-2

As a new git user, I am confused with following steps. I was told to do following when I create my new feature branch

first make sure you got latest changes from remote (git fetch / git merge)

create new branch my-branch and check it out

make change

git add .  //stage changes

git commit -m "my message"

repeate above as many times as needed

Then when ready to push my completed work to remote, make sure you got the latest remote changes into your new branch my-branch

git fetch origin //get latest changes from origin

git merge origin/my-branch // merge the latest changes on remote into my-branch

git push origin my-branch // push my branch to remote

But reading a git tutorial, I see they recommend to push new branch to origin right after creating it using

git push -u origin my-branch

then work on your changes, stage, commit

then push like this

git push

Am I being told to do it incorrectly?

Which method is better and why?

cd491415
  • 823
  • 2
  • 14
  • 33
  • Possible duplicate of [How do you create a remote Git branch?](https://stackoverflow.com/questions/1519006/how-do-you-create-a-remote-git-branch) – phd Aug 26 '18 at 22:39
  • https://stackoverflow.com/search?q=%5Bgit%5D+what+push+-u+does – phd Aug 26 '18 at 22:39

1 Answers1

2

-u switch (short for --set-upstream) makes git on your computer remember that origin/my-branch is the upstream for your local my-branch. If you use it, you will subsequently be able to do simply git push on the same branch, and git will know where to push it. Otherwise you'll have to specify the remote name and remote branch name every time you push.

So, answering your question: using -u is what you want in most cases, unless you are sure that you are not going to push that local branch to that remote branch again (for whatever reasons you might have for that).

Mikhail Burshteyn
  • 4,762
  • 14
  • 27
  • Thanks Mikhail, that explains a lot. But still unclear on point that other than "remembering" what branch to push to remote by use of u switch, my workflow is equally good or I am missing something? I am particularly unsure about my 2nd step where I merge origin into my-branch, then push my-branch to origin? – cd491415 Aug 26 '18 at 20:05
  • 1
    With the workflow you've posted, there would be no other difference besides simpler pushing. – Mikhail Burshteyn Aug 26 '18 at 20:37
  • @cd491415 Would you mind accepting my answer please? :) – Mikhail Burshteyn Aug 26 '18 at 21:00
  • @MikhailBurshteyn It is always is only simpler pushing, isn't it - this is not just for the workflow posted. – tymtam Aug 27 '18 at 03:13