DEV Community

Git Branching, Step-by-Step

Funda on March 18, 2018

"Stop. Collaborate and Listen." ~ V. Ice Enter fullscreen mode Exit fullscreen mode It is highly recommen...
Collapse
 
ben profile image
Ben Halpern

Nice post Funda.

For STEP 4, I'd also suggest git add -p as a means of deciding which files to include. It lets you step through all your changes and give a y or n to decide what to stage.

Collapse
 
thadevelyouknow profile image
Funda

I love that. I am trying to be absolutely minimal (MVP) here, since this was written for the classroom & there is always a lot of confusion when students first encounter this. Perhaps I will add links to handle more shortcuts, best practices, and features.

Collapse
 
ben profile image
Ben Halpern

👌

Collapse
 
rolgalan profile image
Roldán Galán

This is a nice article, however steps 9 and 10 should be swapped.

First you pull the updated master and then you remove the feature branch. Otherwise you will need to force branch delete (git branch -D) because your local repo doesn't know yet that the branch is already merged. And forcing is not nice ;).

Cheers!

Collapse
 
thadevelyouknow profile image
Funda • Edited

yes this is right --- I will edit! And you are correct that forcing anything isn't nice at all.
Thank you for the improvement!

Collapse
 
jpgoldberg profile image
Jeffrey Goldberg

Should there be a git merge master between steps 6 and 7? I try to resolve conflicts, if any, before I submit the PR.

Collapse
 
marioestradarosa profile image
Mario Estrada • Edited

Jeffrey,

Your question is valid 100%. after git checkout mybranchname we should perform a git merge master. It is at this stage that conflicts may appear and you have a chance to fix them. This way you make sure your branch won't conflict with the remote master branch.

Even more, when several developers are working release updates constantly to the master branch, we advise them to perform this sync process often to see how your changes operates in the whole application.

Collapse
 
thadevelyouknow profile image
Funda

the merge happens on the website not from the terminal. This is for students to be able to visualize it in the GUI.

Collapse
 
jrock2004 profile image
John Costanzo

So are you saying that git pull origin master is not required? Why would I pull master locally then just push my branch up?

Thread Thread
 
thadevelyouknow profile image
Funda

git pull origin master is being executed again at step 5. when you push to your branch it creates the pull request on the github website.

Collapse
 
thadevelyouknow profile image
Funda

Updated with changes!

Collapse
 
_shahroznawaz profile image
Shahroz Nawaz

Hey Funda nice effort you've put in the article. I've also written an article on Manage Branches and Resolve Conflicts. Your Feedback would be welcome :)

cloudways.com/blog/manage-branches...

Collapse
 
chiangs profile image
Stephen Chiang

Hi Funda,

Shouldn't one pull from the master after committing changes to your branch so that you are up to date and can resolve and merge conflicts before doing the PR?

Collapse
 
thadevelyouknow profile image
Funda

you mean switch to master, 'git checkout master', then switch back to branch and 'git push origin branch'? Yes...this is what I do, but I found with students too much switching caused confusion. It's either extra switching or extra merge conflicts.

Collapse
 
chiangs profile image
Stephen Chiang • Edited

Maybe I'm confused, but I thought that if others have been working on their own branch and in the process of working on mine, a few PRs have been merged...so before I push to my remote and initiate a PR, I should git pull from the current master to my branch?

Kind of like this:

$ git add .
$ git commit -m "comments"
$ git pull origin master
$ git push origin myBranchName

Then open a PR.

Thread Thread
 
sergiopicciau profile image
Sergio Picciau

You can so you will eventually get conflicts on your feature branch and solve them before pushing to remote.

Thread Thread
 
thadevelyouknow profile image
Funda

that's in the steps now, edited ;-)

Thread Thread
 
chiangs profile image
Stephen Chiang

I see that in Step 5 you switch back to master to pull to your local copy of master, but how does that update your local branch without pulling master to your local branch?

I guess I still don't really understand why you would switch back to master and then back to branch. Thanks!

Thread Thread
 
leahein profile image
Leah Einhorn

Valid question. Just doing git pull origin master in Step 5 will NOT update your local branch. This tutorial is missing a step between 6 and 7. Essentially after you git pull origin master in Step 5, and git checkout mybranchname in step 6, you will run git merge master to combine the master branch changes into your feature branch, named mybranchname. Hope that helps!

Collapse
 
idanarye profile image
Idan Arye

You skipped a step between 4 and 5 - you forgot to create the pull request.

Collapse
 
thadevelyouknow profile image
Funda • Edited

It’s implied in the ‘keep clicking green buttons’ part after you go to the Github site. I have edited this now to assume reader has collaborator permissions. You are right that if this were an open source project, the user would have to create a pull request. But these steps are for students who are just learning to become team collaborators, and for them, the pull request is created when you push origin mybranchname in terminal. Thank you!

Collapse
 
chiangs profile image
Stephen Chiang

I really like using the following to flip back and forth between the two most recent branches. Saves time and having to remember some complicated branch name!

git checkout -
Collapse
 
itachiuchiha profile image
Itachi Uchiha

Thanks, Funda. This is a nice post. Some developer who started to git as a beginner should read this post. I'll share.

Collapse
 
thinkhuang profile image
Third_Thinker

I love this post with that style,especially the beginning of group pictures! HaHa

Collapse
 
saschadev profile image
Der Sascha

Nice article! I suggest that in the first step a git pull -r will make a better branch highway.

Collapse
 
kathcatbc profile image
Kathleen Catlett

Sweet - I am going to share it with my class

Collapse
 
felipperegazio profile image
Felippe Regazio

simple and useful