loading...

Git Branching, Step-by-Step

thadevelyouknow profile image Funda Updated on ・3 min read

git

IF YOU HAVE COLLABORATOR PERMISSIONS, DO THIS:




STEP 1

Run

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.

STEP 2

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

STEP 3

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.

STEP 4

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

STEP 5

I add my changes:

git add file-name-here

STEP 6

Then I commit the changes:

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

STEP 7

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

STEP 8

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

STEP 9

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

STEP 10

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!

STEP 10

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

STEP 11

I go to my remote repository github.com

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.

STEP 12

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.

STEP 13

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.

STEP 14

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

git checkout master

STEP 15

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.

STEP 16

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

git branch -d mybranchname

STEP 17

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

Rinse. Repeat.

Discussion

pic
Editor guide
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 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.

Collapse
ben profile image
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 Author

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

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 Author

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

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

Collapse
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.

Collapse
chiangs profile image
Stephen E. Chiang

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

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 Author

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 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 -
Collapse
aligoren profile image
Ali GOREN

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

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
thinkhuang profile image
Third_Thinker

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

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