Git

How Push to Specific Branch in Git

While working with a team on a project in Git, you often work on a local repository. In such a scenario, when any changes are made, you have to commit to the local branch and the remote repository. For the corresponding purpose, it is required to push the added changes through branches to the remote repository.

This guide will discuss the procedure of pushing to a specific branch in Git. Let’s get started!

How Push to Specific Branch in Git?

Follow the below-given method to push from the Git local repository to the “GitHub” remote repository.

Step 1: Launch Git Bash
At first, search for the “Git Bash” command line using the “Startup” menu and launch it:

Step 2: Navigate to Specified Directory
Next, move to the local directory from where you want to make some changes:

$ cd "C:\User\nazma\testing"

Here, “testing” is our Git local repository that is already created:

Step 3: Initialize Repository
Initialize the repository or directory using the “git init” command. This command will transform the current directory into the Git repository:

$ git init

Step 4: Add Files
We will add all untracked files to the newly initialized repository by specifying the “.” option in the “git add” command:

$ git add .

Step 5: Check Status
Next, check the status of the Git repository:

$ git status

The output will show enlist all files that are added to the staged area and ready to commit:

Step 6: Commit Changes
After that, execute the following command to commit the changes in the local branch with any message:

$ git commit -m "initial commit"

Here, “-m” is used as a flag to add “initial commit” as a message:

Step 7: Copy Remote Repository URL
Next, go to the browser, open your “GitHub” repository, and copy its URL:

Step 8: Add Local Repository to Remote Repository
Now, paste copied URL into the Command line with the “git remote” command as follow:

$ git remote add testing https://github.com/itslinuxhint/testing.git

Here, “testing” is our repository name:

After executing the above command, your specified local repository will be added to the remote Git. In our case, we have already added it to Git, which is also the reason behind the encountered error:

Step 9: Push Local Repository Content to Remote Repository
Push the content of the local repository to a particular branch of Git remote repository:

$ git push -u testing master

Here, “-u” flag which is equivalent to “-set-upstream” is used to maintain the tracking reference, “testing” is our remote repository and “master” is a remote branch of it in which we want to push our files:

The below-given output indicates that our files are successfully pushed to the “master” branch of the remote repository:

Step 10: Verify Pushed Content
Lastly, open your remote GitHub repository and verify the pushed files in the selected branch:

We have provided the easiest method of pushing the files to a specific branch in Git.

Conclusion

To push to a specific branch in Git, open Git Bash and navigate to the directory from which you want to push files to the remote branch. Then, initialize the directory using the “$ git init” command. Next, run the “$ git add .” command to add all files. Then, check repository status and execute the “$ git remote add” command. Lastly, push the file to a specific branch with the “$ git push” command. This guide illustrated the procedure of pushing to a specific branch in git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.