0
0
Gitdevops~5 mins

Pushing new branches to remote in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a new branch in your local Git repository, it only exists on your computer. To share your work or back it up, you need to send this new branch to the remote repository where others can access it.
When you start working on a new feature and want to share it with your team.
When you fix a bug in a separate branch and want to push it to the remote for review.
When you want to back up your local branch to the remote repository.
When you want to create a pull request from your new branch on the remote.
When you switch to a new branch and want to make sure it exists on the remote.
Commands
This command creates a new branch named 'feature-login' locally so you can work on a new feature separately.
Terminal
git branch feature-login
Expected OutputExpected
No output (command runs silently)
Switches your working directory to the new 'feature-login' branch so you can start making changes there.
Terminal
git checkout feature-login
Expected OutputExpected
Switched to branch 'feature-login'
Pushes the new 'feature-login' branch to the remote repository named 'origin' and sets it to track the remote branch for easy future pushes and pulls.
Terminal
git push -u origin feature-login
Expected OutputExpected
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'feature-login' on GitHub by visiting: remote: https://github.com/example/repo/pull/new/feature-login remote: To https://github.com/example/repo.git * [new branch] feature-login -> feature-login Branch 'feature-login' set up to track remote branch 'feature-login' from 'origin'.
-u - Sets upstream tracking so future git push and pull commands work without specifying the branch.
Lists all remote branches to verify that 'feature-login' was successfully pushed to the remote repository.
Terminal
git branch -r
Expected OutputExpected
origin/HEAD -> origin/main origin/feature-login origin/main
-r - Shows remote branches only.
Key Concept

If you remember nothing else from this pattern, remember: use 'git push -u origin branch-name' to push a new branch and set it to track the remote branch for easy future updates.

Common Mistakes
Running 'git push origin feature-login' without the '-u' flag.
This pushes the branch but does not set upstream tracking, so future pushes require specifying the branch name every time.
Use 'git push -u origin feature-login' to push and set upstream tracking in one step.
Trying to push a branch that does not exist locally.
Git will return an error because it cannot find the branch to push.
Create and switch to the branch locally first using 'git branch branch-name' and 'git checkout branch-name' or 'git switch -c branch-name'.
Not switching to the new branch before pushing.
You might accidentally push the wrong branch or no branch at all.
Always switch to your new branch with 'git checkout branch-name' before pushing.
Summary
Create a new branch locally with 'git branch branch-name'.
Switch to the new branch using 'git checkout branch-name'.
Push the new branch to the remote and set upstream tracking with 'git push -u origin branch-name'.
Verify the remote branch exists with 'git branch -r'.