Default Behavior of “Git Push” without a Branch Specified

Better Stack Team
Updated on July 25, 2024

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

  1. Without push.default Set: If you haven't set push.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 push master to origin/master.
    • 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, and feature2, 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.
  2. With push.default Set: If you explicitly set push.default in your Git configuration, it will dictate the behavior of git 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 or git config --help for specifics on each setting.

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.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github