Git Push Tracking Branches and Push Strategies

In the previous blog, we pushed new commits and branches to GitHub.
Now, let’s take it a step further and learn how to set up tracking branches and configure push strategies.


Problem Statement

When you run:

git push

You expect your commits to be pushed to GitHub. But Git doesn’t always know where to push them. That’s why we need tracking branches.


Setting Up Tracking for Main Branch

Switch to main:

git switch main

Check mapping:

git branch -vv

👉 Output: main is not linked to any remote branch yet.

Now, link it using:

git push --set-upstream origin main

or simply:

git push -u origin main

✅ Now your local main is mapped to remote origin/main.

Check again:

git branch -vv

Output now shows:

main   abc123 [origin/main] commit message

This means git push or git pull will work without extra arguments.


Adding Commits After Tracking

Make a new commit:

echo "new line" >> file.txt
git commit -am "Added new line"

Now just run:

git push

✔️ Git automatically pushes main to origin/main.


Setting Up Tracking for a Branch with a Different Remote Name

Create and switch to feature1:

git switch feature1

Add a commit, then try:

git push

❌ This fails because feature1 is not mapped yet.

Map it explicitly:

git push -u origin feature1:audiocall
  • Local branch → feature1
  • Remote branch → audiocall

Now feature1 is tracked with origin/audiocall.


Push Strategies in Git

By default, Git uses push.default = simple.

1. simple (default)

  • Git pushes only if local and remote branch names are the same.
  • Safer for beginners.

2. upstream

  • Git pushes to the configured upstream branch, even if names differ.
  • More flexible for advanced setups.

Configuring Push Strategy

Change push strategy:

git config push.default upstream

Check:

git config push.default

Output: upstream

Now git push works even if local and remote branch names are different.


Visual Recap

  • Local main → mapped to origin/main
  • Local feature1 → mapped to origin/audiocall
  • With push.default = upstream, git push works seamlessly.

Summary

  • Use git push -u origin branch to set up tracking branches.

  • git branch -vv shows which local branch is linked to which remote branch.

  • Push strategies:

    • simple → safer, requires same names
    • upstream → pushes to upstream regardless of branch name

Coming Up Next

In the next blog, we’ll dive deeper into the git push command and explore more options like force push, tags, and deleting remote branches.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉