A Git Branching & Stashing guide

In this post we’ll cover how use Git “branching” and “stashing”. This is part of a Git Tutorial Series. See all the Git tutorials here.

Why use Branching?

Branching allows you to do work outside of the “master” branch. The master branch is always supposed to work. As you’re developing you’ll probably introduce new bugs and test different ways of doing things. You don’t want to do this in the master because you run the risk of breaking things. Branches also allow you to work on different features simultaneously or even test out different directions you may want to take the project.

How to git branch

First get setup with a regular git repository and create a file and commit for your master branch. We’ll do that together if you want to follow along with these steps:

  1. Make a project directory: gitTest
  2. cd into gitTest/
  3. initialize git repository: $ git init
  4. Create folder with “hi” as contents: $ echo "hi" > master.txt
  5. $ git add .
  6. $ git commit -m "first commit"

Now let’s create a new branch named “dev” and do all of our dev work in the “dev” branch. We’ll only “merge” code to master once it’s tested and working. You can create a branch in two ways. Do Either of the following (but not both)

The LONG way:

  1. $ git branch dev (This creates the branch named “dev”
  2. $ git checkout dev (checks out the dev branch)

The SHORT way:

  1. $ git checkout -b dev (Creates AND checks out branch in one command)

Now make some changes… Add files. add some new text to those files, whatever you want. Then commit those changes to the branch you’re on. Let’s do that together by following the steps below:

  1. run command to create a new file: $ echo "doing dev stuff" > dev_branch.txt
  2. $ git add dev_branch.txt
  3. $ git commit -m "added dev_branch.txt"

Ok, we’ve added a new file and committed it to our git repository in the “dev” branch. Look in a text editor to see that the file and text inside of it exist:

Now that our changes have been committed, let’s go back to our master branch. Run the following command:

$ git checkout master

The file should have disappeared, or if it’s already open in your text editor then it may say that the file is deleted. That’s because all your changes are still back over in the dev branch. If you run $ git log --oneline you won’t see our dev_branch commit either because all of that is back in the dev branch. See below. Our recent changes are gone:

We could delete those changes permanently by deleting our branch. If you had a branch named “branch_name” you could delete it with the command below:

$ git branch -d branch_name

If you want to “merge” the code from the dev branch into your master branch then we can do that with the steps below:

  1. Commit all changes in the branch that you want to merge to master. (We already committed the “dev” branch changes. So they’re ready to be merged into master.
  2. Switch to the master branch with $ git checkout master (We’re already in master though)
  3. Run $ git merge dev from the master branch.

That’s it! Now all the committed changes inside the “dev” branch have been merged into the master. Now you can switch back to the dev branch and continue coding. If you mess up really badly just discard the changes.

Not too complicated right? If you wanted to push all your branches up to a remote repository like Github, then you’d use the following command: $ git push REMOTE –all where REMOTE is the name of the remote. (We’ll cover remotes later) I almost always use “origin” so the command for me would look like this:

$ git push origin --all

Git Stash

Git stashing is when you need to switch branches or do something else, but you’re not ready to make a commit, so you “stash” all your code away to come back to later. Let’s give it a try.

From where we left off in the previous section, inside our master branch, let’s do the following steps together:

  1. move back into our dev branch: $ git checkout dev
  2. Edit the contents of master.txt file via terminal: $ echo "an edit to master.txt" > master.txt
  3. Stage the changes, but do NOT commit them: $ git add .
  4. Stash those changes away for later use. $ git stash (I was using a different folder when I took the picture below. So pretend “gitStash” is “gitTest” when looking at the image.

Notice the changes disappeared. They’re gone until we retrieve them from the stash. Now’s a good time to go to some other branch and do whatever you needed to do. When you’re ready, come back to the dev branch and retrieve your stash.

Note: $ git stash will NOT store un-tracked files. You must add them with $ git add file_name first.

Okay, let’s go get our stash… To “unstash” the stuff you were working on, run $ git stash pop

Now if I look in my text editor I can see all the files and edits that were stashed away but are back in action, along with any changes that you made since you originally stashed the code.

Note: If you edit files that have been stashed then you will encounter a “git conflict”, which is not covered until later in this tutorial series.

You can have multiple stashes at a time. To keep them organized, you may want to save a stash with a message using the command like so:

$ git stash save "message to save stash with"

So let’s say I have a couple stashes stored away, and I want to get back to one of them. You can do that by first listing out the available stashes:

$ git stash list

Since we’ve only stashed once, we’ll only have one stash to choose from. Choose the stash you’d like to get back to. If I wanted to get the stash@{0} I’d just run the command like below:

$ git stash apply 'stash@{0}'

By default $ git stash will not store untracked files. To stash untracked files use the -u flag

$ echo "an untracked file" > untrackedFile.txt
$ git stash -u #this stashes away untracked files too.
$ git stash pop #get the stash back.

That’s pretty much all you need to know to stash away changes and use branches in git. To learn about all the use cases of Stash be sure to check out the documentation. check out our other Git tutorials.

Want More Tutorials?

Get our best crash courses and tutorials

(Source Code Provided)

Leave a Comment

Your email address will not be published. Required fields are marked *

want more details?

Fill in your details and we'll be in touch

%d bloggers like this: