Ok, I have been using git on and off for years, and have read books, and have read hundreds if not thousands of posts, but I still don't know the "correct" way to create a branch and push it back, and have spend hours trying to fix git when it goes wrong.
So lets say on github is a master branch. I want to create a feature branch of master branch, then push my feature branch back to github, then do a pull request for someone to merge it. This should be easy, but every day we struggle and have no fool proof set of steps (a recipe)
Step one is to clone the repo. I always do this with tortoise git (windows). I have found that if I use https I can never push back (get 403), I have to use git@... as the repo URL. No idea why, but I have a working solution.
I can then create a branch in 1 of 3 ways.
- create the branch in visual studio code (just enter the name)
- create the branch using tortoise (there are lots of options, I leave them all default)
- create the branch in git bash: "git checkout -b mybranch"
- "git branch my_branch < remote-name >/mybranch" (assume remote is always "origin")
However, I have also seen the following recommended ways to create branches (with the corresponding checkout after)
- git branch --set-upstream my_branch origin/my_branch
- git branch --set-upstream-to origin my-branch
Do all of these do exactly the same thing? Are they all "safe"?
Next is where the problems come in, how to push the branch to github.
I have read posts which say each of these is the correct way:
- git push origin
- git push -u origin
- git push -u origin master
- git push -u origin/mybranch
- git push -u origin mybranch
Which is correct (i.e. least likely to result having to start all over again with a new clone)?
If I do the push with visual studio code or tortoise, which of the above is it actually doing?
I understand that -u = --set-upstream, but what does this actually mean? If I always need it, why does it not do it by default? there are lots of posts asking what this means, but I have not found an answer. Does upstream mean origin?
I admit the problem is my brain - I have been using and administering SVN, SCCS, CVS,Clear Case, VSS for 30 years and never had issues, but git is another beast.
The next step is how to we create a pull request? The tutorials only seem to cover doing this through the github UI. does this mean this is not a feature of git? I.e. we cant do it on the command line, it has to be done through the web?
If we create a pull request via github web interface, who gets notified, and how?
Lets say that we are told to merge the feature branch into the master ourselves (without doing a pull request), what is the safest recipe for doing this? Searching the web, this is one answer:
git checkout mybranch
git pull
git checkout master
git pull
git merge --no-ff --no-commit mybranch
git status
git commit -m 'merge test branch'
git push
Is this this the most fool proof (safest) recipe we can follow?