Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Git Branches and Merging!
You have completed Git Branches and Merging!
We've looked at pushing commits briefly in a previous course. But that was before we had discussed branches at all, so we were only able to push the `master` branch. In this video, we'll show you how to push any branch, not just `master`.
Run the following commands in the repo you're using as a local repo:
- I want to create a new branch to add more letter conversions.
- I want to base this new branch off the
master
branch, so I'll check that out first:git checkout master
- Now let's create and check out a new branch:
git checkout -b more-letters
- I'll edit
decoder.rb
and add a new letter conversion:
24 => 'X',
- Now I'll stage the file:
git add decoder.rb
- And commit it:
git commit -m 'Add X conversion'
- Now I want to push this new branch to my remote repo. But I can't just run
git push
by itself because there's no remote branch to push to:git push
$ git push
fatal: The current branch more-letters has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin more-letters
- Local branches aren't automatically associated with a remote branch.
- When you want to push a local branch, you need to specify a remote branch to push to.
- So I'll need two more arguments to
git push
- a remote repo name, and a branch name:git push origin more-letters
- You'll see in the output that it pushes the commits, and creates a
more-letters
branch in the remote repo.
$ git push origin more-letters
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 356 bytes | 356.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /Users/jay/th/decoder
* [new branch] more-letters -> more-letters
- Let's confirm that the push worked.
- Let me change to my remote repo:
cd ../decoder
- I'll be able to check out the
more-letters
branch that we just pushed:git checkout more-letters
- And if I run
git log
, we'll see the commit I just pushed:git log
- Let me change to my remote repo:
- Before we move on, I need to warn you about something for those of you following along on your computers.
- If you've checked out the
more-letters
branch in the repo that we're treating as a remote repo, and you go back to the other repo and try to push commits to it again, you'll get an error: "refusing to update checked out branch". - So be sure to take a moment and check out a different branch, like
master
:git checkout master
- This will not be an issue if your remote repo is on a hosting service like GitHub, because no branches will be checked out there.
- If you've checked out the
"Refusing to update checked out branch" error
If you're trying to push to a repo that is in a different directory on your local computer, you may see an error like this:
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 355 bytes | 355.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/more-letters
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/jay/th/decoder
! [remote rejected] more-letters -> more-letters (branch is currently checked out)
error: failed to push some refs to '/Users/jay/th/decoder'
If this happens, do the following:
- Change to the directory for the repo you're using as a remote:
cd ../decoder
- Switch to a different branch, so that the branch you're trying to push to isn't checked out:
git checkout master
Now you should be able to change back to your other repo and push successfully.
Note that you won't have this issue with remote repos on hosting services like GitHub, because no branches will be checked out there.
Setting up a tracking branch as you push
- The
more-letters
branch in our local repo hasn't been set up as a tracking branch. - So each time we want to push
more-letters
, we're going to need to specify the remote repo and branch again. - Change back to the local repo:
cd ../decoder-local
- Ensure the
more-letters
branch is checked out:git checkout more-letters
- If we run
git push
and add the-u
option, and specify the remote repo and branch name one more time, that will set upmore-letters
to track theorigin/more-letters
remote branch for us:git push -u origin more-letters
- From then on
git push
will work on themore-letters
branch all by itself:git push
- And
git pull
will work too:git pull
Now, when you add new commits to the more-letters
branch, you'll be able to push them simply by typing git push
!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up