Regardless of whether you’re using GitFlow, GitLab flow or GitHub flow, all local software development should be done on local, isolated feature branches. Branch base development encourages developers to experiment, be bold and discover innovative solutions to problems.
But how does a developer take the brilliant code they’ve written in a newly created Git branch and push to a remote GitHub repository? It’s not hard, but you do need to run a somewhat esoteric upstream branch Git configuration command before pushes to the remote GitHub repo become happenstance.
Push a new Git branch to a remote repo
The steps to follow in order to push new Git branches to remote repos such as GitHub, GitLab or Bitbucket are as follows:
- Clone the remote Git repo locally
- Create a new branch with the branch, switch or checkout commands
- Perform a git push with the –set-upstream option to set the remote repo for the new branch
- Continue to perform Git commits locally on the new branch
- Simply use a git push origin command on subsequent pushes of the new branch to the remote repo
New branch to remote Git repo commands
To create a new local branch to be pushed to the remote GitHub repo, just run a Git branch, switch or checkout command. There are many ways to create branches in Git.
[email protected]/c/remote/push (main) git switch -c new-branch
A git branch -a command will verify that the new Git branch to be pushed to the remote GitHub repo was indeed created locally.
[email protected]/c/remote/push (new-branch) git branch -a main * new-branch remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/new-branch
Note that in this example I use the git switch command to create and move to a new branch, rather than the git checkout command. The git switch replaced checkout in a 2020 Git release.
New Git branches and upstream repos
With a new branch created, the –set-upstream switch must be run the first time a push is performed. This step tells the new branch which remote repository to use every time it synchronizes its commit history.
[email protected]/c/remote/push (new-branch)
$ git push –set-upstream origin new-branch
Enumerating objects: 3, done.
* [new branch] new-branch -> new-branch
Branch ‘new-branch’ set up to track remote branch ‘new-branch’ from ‘origin’.
Failure to perform the –set-upstream step will causes pushes of the new branch to the remote repo to fail with the following error:
fatal: The current branch has no upstream branch
New Git branch pushed to GitHub
A quick refresh on the project’s landing page on GitHub shows the new Git branch has been pushed to the remove successfully.
Once the remote GitHub repository is set as the upstream target for the new Git branch, push an pull operations can be performed normally with a simple git push origin command.
Ongoing push and pull commands for GitHub
The full set of commands used in this example are as follows:
[email protected]/c/remote/push (new-branch) git clone https://github.com/learn-git-fast/git-branch-examples.git 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 "Are we not gender neutral people? We are Devo?" git push --set-upstream origin new-branch [email protected]/c/remote/push (new-branch) touch eden.html git add . git commit -m "Eden added" git push origin