Learning Git and GitHub [Part Three]

Automating, Git Branch Pushing

Automating Git Branch Pushing

Hello! Welcome to the third part of the Learning Git and GitHub series. In this post we will be covering how to automate the process of pushing a new branch to GitHub. This post will be using the same repository my-first-repository that we created in the first post.

Why can this be helpful?

When working on a project, you may find yourself creating a lot of branches. This can be for a number of reasons, such as creating a new feature, fixing a bug, or even creating a new blog post. When you create a new branch, you will need to push it to GitHub so that it is available for others to see and collaborate on. This can be a repetitive process and can be automated using a few simple steps.

For example, when we create a branch and try and push we get the following message.

# git add * && git commit -m "manual git branch push" && git push
[feature/manual-git-pushing 9a03f42] manual git branch push
 1 file changed, 1 insertion(+)
 create mode 100644 git-push-file-one
fatal: The current branch feature/manual-git-pushing has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/manual-git-pushing

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

As we can see here, we have to change the upstream branch to be able to push, and at times when your making multiple changes and branches, Well I forget and then wonder why my changes are not showing up on GitHub. 😅

So lets fix this!

Automating the process

Looking at the local git docs git push help we can see that there are two options true or false

Local Docs Path: file:///C:/Program%20Files/Git/mingw64/share/doc/git-doc/git-push.html

clear

1
git config --global push.autoSetupRemote true

Once this is set to true we can now create a new branch and push it to GitHub without having to set the upstream branch.

1
git checkout -b feature/automated-git-pushing

Hey! Don’t forget to add a file and commit your changes before pushing!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[feature/automated-git-pushing f3ae172] automated git branch push
 1 file changed, 1 insertion(+)
 create mode 100644 git-push-file-two
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'feature/automated-git-pushing' on GitHub by visiting:
remote:      https://github.com/builtwithcaffeine/my-first-repository/pull/new/feature/automated-git-pushing
remote:
To https://github.com/builtwithcaffeine/my-first-repository.git
 * [new branch]      feature/automated-git-pushing -> feature/automated-git-pushing
branch 'feature/automated-git-pushing' set up to track 'origin/feature/automated-git-pushing'.

Finally, you can see your git global config by running the following command.

1
git config --global --list

Summary

So there we have it, we have now automated the process of pushing a new branch to GitHub. This can be helpful when working on a project and creating multiple branches. - This has been something, I’ve been meaning to do for a while now, and I’m glad I finally got around to it. 😅

Share with your network!

Built with Hugo - Theme Stack designed by Jimmy