How can I push a specific commit to a remote, and not previous commits?

Viewed 671272

I have made several commits on different files, but so far I would like to push to my remote repository only a specific commit.

Is that possible?

8 Answers

The simplest way to accomplish this is with two commands.

First, get the local directory into the state that you want. Then,

git push origin +HEAD^:someBranch

removes the last commit from someBranch in the remote only, not local. You can do this a few times in a row, or change +HEAD^ to reflect the number of commits that you want to batch remove from remote. Now you're back on your feet, and use

git push origin someBranch

as normal to update the remote.

You could also, in another directory:

  • git clone [your repository]
  • Overwrite the .git directory in your original repository with the .git directory of the repository you just cloned right now.
  • git add and git commit your original

I just used

git cherry-pick 'sha id of the commit'

and then

git push

In my case my local branch name was 'master' and the remote default branch name was 'master' too.

Related