DEV Community


Posted on • Updated on

Git Branching, Step-by-Step


Enter fullscreen mode Exit fullscreen mode



No. Don't run away.
In your terminal, type:

git status

As often as you want to. It will give you clues about your next steps in any type of git workflow. git status, early and often.


I make sure I am in the master branch of my team's shared repository & that I have the LATEST VERSION:

git pull origin master


I make my very own branch. You can make one from the Github site but I prefer to do it in Terminal/bash. I name it a name with no spaces, like: "mybranchname". It's best to name it after a feature or fix you're working on.

git checkout -b mybranchname

Now I am in my branch, not the master, so it is safe to make changes.


I code away in my code editor, tappity-tap.


I add my changes:

git add file-name-here


Then I commit the changes:

git commit -m 'my awesome improvements to our life-changing app'


If I only want to push changes to the local copy of my branch (see step 8 below, for the better method)

git push origin mybranchname


If I want to push changes to a remote reference to my branch, one where changes are being tracked every time. (This is what I want!)

git branch --set-upstream-to=origin/mybranchname mybranchname


If you ran git status you would know now to:

git push

Warning: you can only use 'git push' if you set the origin mybranchname to track your mybranchname locally. otherwise use the method in step 7


But hold up. I want to resolve conflicts with the master branch, in case anyone has been working in it, and I want to resolve them before I make a Pull Request.
So I:

git pull origin master - yes from right inside my branch!

This will both fetch my parent branch (in this case master, but it could be some other parent development branch, too) and merge it with my current branch! Remember to fix any merge conflicts now!


If you ran git status again, you would know to:

git push to push the merged changes to your local branch.

If I get a prompt that I need a commit message to explain the merge, I can exit the editor with

Shift Z Z


I go to my remote repository

Once there, I see in our shared repo, a 1 (or any number of requests) under "Pull Requests", so I click to go to that page.


I click on my pull request to open it up, and then I click the green confirmation button, and click again, until I have satisfied all of the options to confirm the merge. If there are conflicts, I resolve them before confirming everything.


No more green, you will now get an alert that is purple, telling you the merge is complete. It will ask you to delete the branch. Go ahead and do that. It's best to use new, descriptively named branches for bugfixes and features, then delete them after merging.


In my terminal (or git bash for PC,) I switch to my master branch

git checkout master


Just in case, I refresh my master branch again using git pull origin master, to make sure I am up to date with my changes and also with any other changes that people made concurrently.


In my terminal, I delete the local version of the branch I SUCCESSFULLY merged & had deleted remotely on

git branch -d mybranchname


I want to make a new branch and start this cycle all over again from STEP 1.

Rinse. Repeat.

Discussion (27)

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.

thadevelyouknow profile image
Funda Author

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.

ben profile image
Ben Halpern


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 ;).


thadevelyouknow profile image
Funda Author • Edited on

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

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.

marioestradarosa profile image
Mario Estrada • Edited on


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.

thadevelyouknow profile image
Funda Author

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

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 Author

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.

_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 :)

chiangs profile image
Stephen E. 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?

thadevelyouknow profile image
Funda Author

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.

chiangs profile image
Stephen E. Chiang • Edited on

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

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

Thread Thread
chiangs profile image
Stephen E. 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!

idanarye profile image
Idan Arye

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

thadevelyouknow profile image
Funda Author • Edited on

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!

chiangs profile image
Stephen E. 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 -
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.

saschadev profile image
Der Sascha

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

thinkhuang profile image

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

kathcatbc profile image
Kathleen Catlett

Sweet - I am going to share it with my class

felipperegazio profile image
Felippe Regazio

simple and useful