Pushing and Pulling Branches
In a shared office, each project has a folder in a cabinet. Anyone can put a new folder in, take a copy of a colleague's folder to work on, add pages to it, and finally remove it when the project is finished.
Branches on a server work the same way. So far you have pushed and pulled main. In real teams, most work happens on feature branches: you push yours so others can see it or help, a teammate picks it up on their own computer, you both add commits, and when the work is merged, the branch is deleted on the server. Each of these steps is one or two commands you already know, combined in the right order.
In this lesson, you will follow one branch through that whole life.
The Starting Point
The server fee-tracker.git, with clones for Navika and Advitha:
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
cd ../../navika/fee-tracker
git branch -a* main
remotes/origin/mainPush a New Branch
Navika starts receipt printing on a branch. As in "Git Push", the first push names the remote and branch and sets the upstream with -u:
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git push -u origin feature/receiptsTo ../../fee-tracker.git
* [new branch] feature/receipts -> feature/receipts
branch 'feature/receipts' set up to track 'origin/feature/receipts'.The branch now exists on the server, and anyone with access can see it. On GitHub, GitLab and Bitbucket, the website now offers to open a pull request for it, which is the topic of the next module.
A Teammate Picks Up the Branch
Advitha needs to help. Her clone doesn't know about the branch yet, so she fetches, and then switches to it:
cd ../../advitha/fee-tracker
git fetchFrom /home/navika/projects/fee-tracker
* [new branch] feature/receipts -> origin/feature/receiptsgit switch feature/receiptsbranch 'feature/receipts' set up to track 'origin/feature/receipts'.
Switched to a new branch 'feature/receipts'There was no local feature/receipts, but there was origin/feature/receipts, so git switch created a local branch from it and set its upstream in one step. This shortcut works when exactly one remote has a branch with that name. The explicit forms do the same:
| Command | Does |
|---|---|
git switch feature/receipts | Creates the branch from origin/feature/receipts, with upstream (the usual way) |
git switch -c receipts-work --track origin/feature/receipts | The same, with a local name of your choice |
git checkout feature/receipts | The older form of the first command |
Work on the Same Branch Together
The branch now behaves like main did in the previous lessons, with pull and push. Advitha adds a commit and pushes:
echo "Print after payment" >> receipts.txt
git commit --quiet -am "Print a receipt after payment"
git pushTo /home/navika/projects/fee-tracker.git
6545c7a..3546c41 feature/receipts -> feature/receiptsNavika pulls on her copy of the branch:
cd ../../navika/fee-tracker
git pullFrom ../../fee-tracker
6545c7a..3546c41 feature/receipts -> origin/feature/receipts
Updating 6545c7a..3546c41
Fast-forward
receipts.txt | 1 +
1 file changed, 1 insertion(+)Both now have the same two commits on feature/receipts. The rules are the same as for main: pull before you push, and if a push is rejected, pull and push again.
Push Under Another Name
The branch name on the server doesn't have to match the local one. local:remote sends a local branch to a server branch with a different name. Navika keeps a quick experiment locally as try-discount, and shares it as experiment/discount:
git switch --quiet -c try-discount
echo "Discount: 10%" > discount.txt
git add discount.txt
git commit --quiet -m "Try a sibling discount"
git push origin try-discount:experiment/discountTo ../../fee-tracker.git
* [new branch] try-discount -> experiment/discounttry-discount -> experiment/discount shows the mapping. This form is also how you push one branch into another on the server, for example git push origin HEAD:main, though that is rarely needed.
Rename a Branch on the Server
A server branch can't be renamed directly. You push it under the new name and delete the old one. The team wants experiment/discount called feature/discount:
git branch -m try-discount feature/discount
git push --quiet -u origin feature/discount
git push origin --delete experiment/discountTo ../../fee-tracker.git
- [deleted] experiment/discount- [deleted] confirms the old name is gone from the server. Teammates who had the old branch should fetch with --prune and switch to the new name.
Delete a Branch on the Server
When feature/receipts is merged into main, Navika deletes it on the server and locally:
git switch --quiet main
git merge --quiet feature/receipts
git push --quiet
git push origin --delete feature/receiptsTo ../../fee-tracker.git
- [deleted] feature/receiptsgit branch -d feature/receipts
git branch -aDeleted branch feature/receipts (was 3546c41).
feature/discount
* main
remotes/origin/HEAD -> origin/main
remotes/origin/feature/discount
remotes/origin/mainPushing deletes only the server branch, and git branch -d deletes the local one. Other people's copies, like Advitha's origin/feature/receipts, disappear when they run git fetch --prune. On hosting platforms, a "Delete branch" button after merging a pull request does the server side for you.
Syntax
| Command | It is used to |
|---|---|
git push -u origin <branch> | Share a new branch |
git fetch then git switch <branch> | Start working on a teammate's branch |
git switch -c <name> --track origin/<branch> | The same, with your own local name |
git push origin <local>:<remote> | Push a branch under another name |
git push origin --delete <branch> | Delete a branch on the server |
git ls-remote --heads origin | List the branches on the server right now |
Common Mistakes
Looking for a teammate's branch without fetching. Your repository only knows the branches it has fetched. Run git fetch first.
Creating a new branch with the same name instead of switching to the server one. git switch -c feature/receipts makes a fresh branch from where you are, unrelated to the server's. Use git switch feature/receipts (without -c) to start from origin/feature/receipts.
Working on a shared branch without pulling. Your pushes get rejected, and conflicts grow. Pull before you start and before you push.
Deleting only the local branch and thinking it's gone. git branch -d doesn't touch the server. Use git push origin --delete.
Committing on origin/feature/... directly. It is a remote-tracking branch and leads to detached HEAD. Switch to the local branch.
Under the Hood
Every push uses a refspec, <source>:<destination>. git push origin feature/receipts is short for git push origin feature/receipts:feature/receipts. Deleting is a push of nothing to the destination, and the older spelling shows it clearly: git push origin :feature/receipts, an empty source, means the same as --delete.
git ls-remote asks the server directly, without changing anything locally:
git ls-remote --heads origin0e82567959e751aa5e7583d69740a69919a27075 refs/heads/feature/discount
3546c41f07e5c66bc7c912dd81ef52b26dc48e03 refs/heads/mainIt lists each branch on the server with its commit. Unlike git branch -r, which shows your last fetched copies, this is the server's state at this moment.
Interview Corner
How do you push a new local branch to the remote?
git push -u origin <branch>. It creates the branch on the remote and sets it as the upstream. With push.autoSetupRemote=true, a plain git push does the same.
How do you check out a branch that a teammate pushed?
git fetch, then git switch <branch>. Git creates a local branch from origin/<branch> and sets the upstream. The explicit form is git switch -c <branch> --track origin/<branch>.
How do you delete a remote branch?
git push origin --delete <branch> (or the older git push origin :<branch>). Delete the local branch separately with git branch -d, and others remove their copies with git fetch --prune.
How do you rename a branch that is already on the remote?
Rename it locally with git branch -m old new, push it with git push -u origin new, delete the old one with git push origin --delete old, and tell teammates to fetch with --prune.
How can you list the branches that exist on the remote right now?
git ls-remote --heads origin queries the server directly. git branch -r shows the remote-tracking branches from the last fetch, which may be out of date.
Practice
Challenge: is it still there? Advitha's computer still has origin/feature/receipts, which Navika deleted on the server. In Advitha's clone, list the remote-tracking branches before and after git fetch --prune, and ask the server for its branches.
What does Git print? Before you open the solution, decide which names each list contains. Remember that the server branch was renamed too.
Show solution
cd ../../advitha/fee-tracker
echo "Before pruning:"
git branch -r
git fetch --prune --quiet
echo "After pruning:"
git branch -r
echo "On the server:"
git ls-remote --heads originBefore pruning:
origin/HEAD -> origin/main
origin/feature/receipts
origin/main
After pruning:
origin/HEAD -> origin/main
origin/feature/discount
origin/main
On the server:
0e82567959e751aa5e7583d69740a69919a27075 refs/heads/feature/discount
3546c41f07e5c66bc7c912dd81ef52b26dc48e03 refs/heads/mainBefore pruning, Advitha's list still has origin/feature/receipts, deleted on the server, and doesn't have feature/discount, which she has never fetched. After git fetch --prune, both lists match the server: feature/discount and main. git ls-remote shows the same two branches, straight from the server.
Checkpoint. With two clones of one repository, push a branch from the first, pick it up in the second, add a commit in each (pulling in between), then delete the branch on the server and prune it in both.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git push -u origin <branch> | Share a new branch |
git switch <branch> | Start on a teammate's branch after fetching |
git switch -c <name> --track origin/<branch> | Same, with your own local name |
git push origin <local>:<remote> | Push under another name |
git push origin --delete <branch> | Delete a server branch |
git fetch --prune | Remove copies of deleted server branches |
git ls-remote --heads origin | List the server's branches |
Was this tutorial helpful?