DEV Community

Cover image for Automate Upstream Branch Creation in Git
Labby for LabEx

Posted on

Automate Upstream Branch Creation in Git

Introduction

This article covers the following tech skills:

Skills Graph

In a collaborative project, it is common to have multiple developers working on different branches of the same repository. When a developer pushes their changes to a branch that does not exist on the remote repository, the push will fail. This is where upstream branch creation comes in handy. By enabling automatic upstream branch creation on push, developers can avoid the hassle of manually creating the branch on the remote repository.

Automate Upstream Branch Creation

As a developer, you want to automate the process of creating upstream branches on push to avoid the hassle of manually creating the branch on the remote repository.

For this lab, you will fork the https://github.com/labex-labs/git-playground repository to your account, using the git-playground repository on your account to automatically create the upstream branch on push.

  1. On the GitHub website, log in to your account and find https://github.com/labex-labs/git-playground to fork the repository to your account.
  2. On the page for your own forked repository, click the Code button and copy the URL of the repository.
  3. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/your-username/git-playground.git
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
Enter fullscreen mode Exit fullscreen mode
  1. Use the following command to enable automatic upstream branch creation on push:
git config --global push.default current
Enter fullscreen mode Exit fullscreen mode
  1. Push a new branch called new-feature, which does not exist in the remote repository:
git checkout -b new-feature
git push
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the new branch has been created on the remote repository:
git ls-remote --heads origin
Enter fullscreen mode Exit fullscreen mode

This is the result after completing the lab:

<result>

Summary

By enabling automatic upstream branch creation on push, developers can avoid the hassle of manually creating the branch on the remote repository. This lab has demonstrated how to enable this feature using the git config command and how to push changes to a new branch that does not exist on the remote repository.

MindMap


πŸš€ Practice Now: Automate Upstream Branch Creation


Want to Learn More?

Top comments (0)