Push and pull a remote git branch
Remote branches are a fantastic way of corroborating features or bugs that need to be worked on by more than one developer. It makes sense to use remote branches, especially if you don’t have any kind of mechanism to deploy code to your live site other than from your master branch, and in this case, you don’t want to share unfinished or unstable code into your master branch (if currently the master branch is the only active branch that developers share between each other to exchange code).
Hopefully in the future I can discuss the various ways of managing code for small teams.
Enough with that and on with creating a remote branch!
Create a local branch first
I usually begin by creating or working on a local branch, skip this step if you already have a local branch you would like to push up.
git branch <Branch Name>
Push your local branch to a remote location
The following command will push your local branch to your remote repository(origin):
git push origin <Branch Name>
That’s it! your local branch has now been pushed to the remote repository, other developers are now able to pull that branch and start working on it too.
Pull and track a remote branch
In order for other developers to work on your branch, they must first pull references to that branch and then create the remote branch in their local repository.
This is a two step process:
Step 1 – Fetch the remote branch
Firstly, we must fetch the latest references… its a bit like saying:
hey! apparently there have been some updates, some of these updates include a new remote branch, although I’m not going to create that branch, I’m going to update your local repository so it knows about the new branch! Let me know when you are ready to utilise this branch!
Let’s do some fetching:
Now that we have fetched all the latest references, we can now see what remote branches exist:
git branch -r
This will return a list of all the remote branches:
origin/<Branch Name> origin/SomeOtherRemoteBranch origin/SomeOtherRemoteBranch2 origin/SomeOtherRemoteBranch3
Great! This is good news, we can see our remote branch, which means we can create it in our local repository.
Step 2 – Create the remote branch
Since we have fetched references to the remote branch, git is now able to create that remote branch locally so we can start working on code within that branch.
Lets create the remote branch within our local repository:
git checkout -b <Branch Name> origin/<Branch Name>
What exactly is the above command doing? Lets break it down:
git checkout -b is shorthand code to create a branch. <Branch Name> is what we want to call the local branch and finally origin/<Branch Name> is telling git we want to create our new local branch based on the remote branch. Simples.
Git has now created the new local branch, linked it’s references to the remote branch so that every time you do a git push, the remote branch will be updated too
When you run a git push command, by default git will push all updates, across all branches to the remote repository.
You can see how useful this can be when working on the same project within your team, you can isolate code chunks into branches and work on that branch without it affecting your master branch to exchange code.
To overcome the caveat I highlighted above about using the git push command, you can easily change gits default push/pull behaviour so it only pushes or pulls code depending on the branch that you are on:
Push current branch only
git config --global push.default current
Pull current branch only
git config --global pull.default current
Now when you run git push or git pull, git will respectively update changes to the branch you are currently on 😎