How to Push a Specific Commit to a Remote Repository

John Wachira Feb 02, 2024
How to Push a Specific Commit to a Remote Repository

This article outlines the process of pushing a specific commit to a remote repository in Git. Supposing we have a Git repository with several commits and we only need to push one commit, how would we go about this?

Push a Specific Commit to a Remote Repository

To push a single commit to the remote repository, we use the Git push command in the context shown below.

$ git push <remote> <commit id>:<remote branch>

As always, we will employ an example to illustrate the concept.

The example below shows a Demo repo with four commits, as shown in the image below.

commit history

Suppose we want to push the initial commit to the remote repository. How would we go about it?

Assuming we are pushing to the remote repository for the first time, we will follow these steps.

First, we will add the remote repo to our local repo, as shown below.

$ git remote add origin https://github.com/Wachira11ke/Demo.git

To push the initial commit to the remote, we will run:

$ git push origin 6b0f31a:refs/heads/master

Since we are pushing to the remote for the first time, we need to use the refs/heads/master argument to create a master branch in the remote repository. This command will only push our Initial commit to the remote repo.

What if we want to only push the first commit, i.e., Update Licences?

Running git push origin c87321d:master will not have the desired effect.

This is because Git will push all commits to and including the specified commit. We do not want that, do we?

We need to rearrange the commits so that the first commit is the last. Our Update Licences commit should come immediately after the Initial commit.

We can do this by running the git rebase command in the interactive mode, as shown below.

$ git rebase -i HEAD~3

This command will open a text editor. We will use the pick command and rearrange the commits, as shown below.

git rebase

We can now exit the text editor and complete the rebase. Our commit history should now look like this:

commit history 2

We can now push the commit to the remote repository, as illustrated below.

$ git push origin 20136f7:master

Our remote repository should only contain two commits.

  1. The Initial commit commit
  2. The Update Licences commit.

Let’s confirm our case.

remote repository

There you have it.

In conclusion, the git push <remote> <commit id>:<remote branch> allows us to push a specific commit to the remote repository.

However, the command will push all commits to and including the specified commit. To remedy this, you will need to rearrange your commits with the git rebase command, as we illustrated.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Push