How to push new Git branches to remote repositories on GitHub or GitLab

Whether you use GitFlow, GitLab, or GitHub, all local software development should be done on local and isolated feature branches. The development of the branch encourages developers to experiment, to be daring and to discover innovative solutions to problems.

But how does a developer take the brilliant code they wrote in a newly created Git branch and push it to a remote GitHub repository? It’s not difficult, but you need to run a somewhat esoteric upstream branch Git configuration command before pushes to the remote GitHub repository become a fluke.

Push a new Git branch to a remote repository

The steps for pushing new Git branches to remote repositories such as GitHub, GitLab, or Bitbucket are as follows:

  1. Clone the remote Git repository locally
  2. Create a new branch with the branch, switch or checkout commands
  3. Git push with the –set-upstream option to set the remote repository for the new branch
  4. Continue to perform Git commits locally on the new branch
  5. Just use a git push origin command during subsequent pushes of the new branch to the remote repository

New branch to remote Git repository commands

To create a new local branch to push to the remote GitHub repository, just run a Git branch, switch, or checkout command. There are many ways to branch in Git.

[email protected]/c/remote/push (main)
git switch -c new-branch

A git -a branch The command will verify that the new Git branch to push to the remote GitHub repository has been created locally.

[email protected]/c/remote/push (new-branch)
git branch -a
* new-branch
remotes/origin/HEAD -> origin/main

Note that in this example I am using the git switch command to create and move to a new branch, rather than the git checkout command. The git switch replaced payment in a Git 2020 release.

With the creation of a new branch, the –Installation upstream must be actuated the first time that pressure is applied. This step tells the new branch which remote repository to use each time it synchronizes its validation history.

[email protected]/ c / remote control / push (new branch)
$ git push –set-upstream origin new-branch
Enumeration of objects: 3, done.
* [new branch] new-branch -> new-branch
Branch ‘new-branch’ configured to follow remote branch ‘new-branch’ from ‘origin’.

If the –set-upstream step is not performed, pushes from the new branch to the remote repository will fail with the following error:

fatal: The current branch has no upstream branch

New Git branch pushed to GitHub

A quick refresh of the project landing page on GitHub shows that the new Git branch has been successfully pushed for deletion.

The upstream configuration switch is needed when first pushing a new Git branch to a remote GitHub repository.

Once the remote GitHub repository is set as an upstream target for the new Git branch, push and pull operations can be performed normally with a simple git push origin order.

Current push and pull commands for GitHub

The full set of commands used in this example is as follows:

[email protected]/c/remote/push  (new-branch)
git clone
cd git*
git checkout -b new-branch

[email protected]/c/remote/push (new-branch)
git branch -a
touch devolution.jpg
git add .
git commit -m "We are Devo?"
git push --set-upstream origin new-branch
touch eden.html
git add .
git commit -m "Eden added"
git push origin

Source link

Margie D. Carlisle

Leave a Reply

Your email address will not be published. Required fields are marked *