Git

How to ‘git push’ to Existing GitHub Repository Branch

Sometimes Git developers make changes on the old development project source code files that already exist in the GitHub server after so long when they are required or specifically add changes in the remote branch data locally, then push them to the GitHub server for updating the other project members. They can use the “git push -u -f <target-branch-name>” command.

This write-up will explain how to perform the ‘git push’ to the existing centralized server branch.

How to ‘git push’ to Existing GitHub Repository Branch?

Check out the following steps to perform the “git push” into the GitHub existing remote branch:

    • Redirect to the Git local repository.
    • Display the current repository updated content that needs to push.
    • Switch to the GitHub account, and copy the URL of the desired remote repository.
    • Set a new remote URL to the list.
    • Lastly, use the “git push -u -f <branch-name>” command.

Step 1: Go to Git Local Repository

At first, navigate to the desired local repository by typing out the “cd” command:

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

 
Step 2: List Content

Then, run the “ls” command to view the content of the desired repository:

$ ls

 

Step 3: Copy Remote Repository URL

Next, go to the GitHub server and copy the path of the desired repository to which you need to push the existing GitHub repository branch. For instance, we have copied the path of the “demo” repo:


Step 4: Set Remote URL

Now, execute the provided command to set the “origin” remote with desired GitHub repository’s path:

$ git remote add origin https://github.com/GitUser0422/demo.git

 

Step 4: Push Local Content to Existing GitHub Repository Branch

Finally, add the local content to the existing remote branch by running the “git push” command:

$ git push -u -f origin master

 
Here:

    • -u” option is used to make the remote repository the default for an existing project.
    • -f” flag will force Git to overwrite desired files already in the remote repository with the user’s existing development project files.
    • origin” is the name of the previously added remote name.
    • master” is the target branch to which we want to push the modified local content.

It can be observed that the locally performed changes are successfully pushed to the particular existing remote branch:


That’s all! You have learned how to perform “git push” into the existing remote branch.

Conclusion

To perform the “git push” into the GitHub existing remote branch, first, redirect to the Git local repository and view its updated content which we want to push. Then, switch to the GitHub account, and copy the URL of the desired remote repository. Add a new remote URL to the list and verify it. After that, execute the “git push -u -f <branch-name>” command. This write-up explained the way of pushing to the existing remote branch.

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.