Git

Git Push Local Branch to Remote

Git is the most commonly used decentralized tracking system that records the modifications or changes to a development project over time in a repository. More specifically, the Git “$ git push” command is used in the development workflow to record the changes from the local directory to the remote directory.

This study will explain how to perform Git push from the local branch to the remote.

How to Perform Git Push Operation From the Local Branch to Remote?

To perform Git, push the local branch to the remote, first, run the “cd” command to move to the Git directory. Next, create and switch the branch immediately through the “$ git switch -b <branch-name>” command. Then, go to GitHub, copy the remote repository URL and execute the “$ git clone <remote-url>” command.

After that, run the “$ git branch -r” command to list the remote branches and push the Git local branch to remote through the “$ git push origin <branch-name>” command.

Now, understand the above-discussed procedure!

Step 1: Navigate to Git Repository
Move to the Git local directory through the provided command:

$ cd "C:\Users\nazma\Git\Demo11"

Step 2: Create and Switch Branch
Now, execute the “git branch” command with the “-b” option to create and switch branch simultaneously:

$ git branch -D feature

For instance, we have created the branch name “feature” and switched to it after creation:

Step 3: Copy URL
Next, go to the GitHub account and choose the repository. Then, hit the below-highlighted button and copy the repository HTTPS URL:

Step 4: Git Clone Repository
After that, execute the given-below command to clone the remote repository to the Git local repository:

$ git clone https://github.com/GitUser0422/demo.git

Step 5: List Remote Branch
To only list the remote branches, execute the “git branch” with the “-r” option:

$ git branch -r

Step 6: Execute git push Command
Now, run the “git push” command with the remote and branch name to upload the local changes to the remote repository:

$ git push origin feature

As you can see, the Git local branch is successfully pushed to the Git remote repository:

That’s it! We have offered the easiest way to perform the Git push from the local branch to the remote.

Conclusion

To perform a Git push from the local branch to the remote, first, move to the Git directory. Then, create and switch the branch immediately through the “$ git switch -b <branch-name>” command. Next, go to GitHub, copy the remote repository URL and run the “$ git clone <remote-url>” command. After that, list the remote branches and push the Git local branch to remote through the “$ git push origin <branch-name>” command. This study illustrated how to perform Git push operation from the local branch to the remote.

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.