Default Behavior of “Git Push” without a Branch Specified
The default behavior of git push
without a branch specified depends on the configuration of your Git repository, particularly the push.default
setting. This setting determines the default behavior when you run git push
without specifying the branch explicitly.
Default Behavior Explained
- Without
push.default
Set: If you haven't setpush.default
explicitly, Git's behavior depends on the version of Git you're using:- Git 1.x:
- When you run
git push
without arguments, Git will attempt to push the current branch to a branch with the same name on the remote repository. - For example, if you are on the branch
master
,git push
will pushmaster
toorigin/master
.
- When you run
- Git 2.x and newer:
- Git will push all branches that have a corresponding branch with the same name on the remote repository.
- For example, if you have branches
master
,feature1
, andfeature2
, and they all have corresponding branches on the remote (origin/master
,origin/feature1
,origin/feature2
),git push
will push all these branches to their corresponding branches on the remote.
- Git 1.x:
- With
push.default
Set: If you explicitly setpush.default
in your Git configuration, it will dictate the behavior ofgit push
:push.default
=simple
:- Git will push the current branch to the remote branch with the same name, but only if it has been set as an upstream branch.
- This is the default behavior in Git 2.x and newer versions.
- Other
push.default
settings (matching
,upstream
,current
, etc.):- Each setting dictates a different default behavior for
git push
regarding which branches are pushed and where they are pushed to. Refer to the Git documentation orgit config --help
for specifics on each setting.
- Each setting dictates a different default behavior for
Checking push.default
Setting
You can check your current push.default
setting using the following command:
git config --get push.default
If nothing is returned, it means the default behavior based on your Git version is being used.
Best Practices
- Explicitly Specify Branches: It's generally a good practice to explicitly specify branches when pushing to avoid unintended changes being pushed to the remote repository.
- Review Before Pushing: Always review which branches are being pushed, especially in shared repositories, to avoid pushing unintended changes.
By understanding these default behaviors and configurations, you can effectively manage how git push
operates in your Git workflow.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for us![](/assets/articles/authors/marin-0d19fe482115fe2b15c8429a7ee4eb88d803984eaa7c1d8c7309241049f3d402.png)
Build on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github