Git

How to Push a Specific Commit to a Remote, and not Previous Commits?

Git developers independently work on the local repository. They can create files, new branches, track changes, and then update their repositories by committing the latest changes. When changes are committed, they are permitted to push into the centralized hosting servers. Moreover, specific commits can be pushed into the remote repository when needed by utilizing the “$ git push <remote-name> <sha-hash>:<remote-branch>” command.

This guide will provide the procedure to push a particular commit to a GitHub remote repository and ignore the previous commits.

How to Push a Particular Commit to a GitHub Remote Repository and ignore Previous Commits?

To push a specific commit to a remote rather than all, implement the below-given steps:

Step 1: Go to Specific Git Repository

First, run the “cd” command along with the local repository path and navigate to it:

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

Step 2: View Git Reference Log History

Then, view the Git reference log history by utilizing the “git log .” command:

$ git log .

Here, we have selected the below-highlighted commit which we want to push into the remote:

Step 3: Check Remote URL List

Next, execute the provided command to show the list of remote URLs:

$ git remote -v

Step 4: Display List of All Branches

Now, view the list of all Git branches, including remote and local, with the help of the “git remote” command and “-a” flag for all:

$ git branch -a

As a result, all branches will be displayed, and select the desired remote branch. For instance, we have chosen the “remotes/origin/master” branch:

Step 5: Push Specific Commit

Finally, execute the “git push” command to push the specific commit to the GitHub hosting service:

$ git push origin 894cf22:master

In the above-stated command:

  • origin” is our remote URL name that is used for tracking the remote repository data.
  • 894cf22” is the SHA-hash of the particular commit, which we need to push only into the remote repository.
  • master” is the remote branch name in which we want to push specific commit changes.

As a result, our selected single local commit changes are pushed into the GitHub remote repository:

That’s all! We have efficiently pushed a particular commit to a GitHub remote repository and ignored the previous commits.

Conclusion

To push a specific commit to a Git remote repository, first, move to the particular Git repository and then check the Git reference log history, select the desired commit, and copy its SHA-hash. After that, view the remote URLs and list all existing branches. Lastly, run the “$ git push <remote-name> <sha-hash>:<remote-branch-name>” command. This guide elaborated on the process of pushing the desired commit to previous remote commits.

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.