Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I push specific commit to a remote, without the previous commits?

Tags:

git

I committed code in local repository 2 or more times using

git commit -m "message 1"
git commit -m "message 2"
git commit -m "message 3"

Now I have three commits with following SHA

commit-1 SHA1
commit-2 SHA2
commit-3 SHA3

But I want to push only commit-2 in remote repository using git push.
If I run git push then it will push all commits.
And I also tried following commands:

git push SHA2

but this also pushed all commits.

How to push this only commit-2 in remote repository ?

like image 758
Undefined Behaviour Avatar asked Jan 18 '16 07:01

Undefined Behaviour


People also ask

How do you push a commit to a remote branch?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

Can you push without commits?

No, you must make a commit before you can push. What is being pushed is the commit (or commits).

How do I push only a specific commit in Intellij?

Do one of the following: To push changes from the current branch press Ctrl+Shift+K or choose Git | Push from the main menu. To push changes from any local branch that has a remote, select this branch in the Branches popup and choose Push from the list of actions.

How do I pull a specific commit from a remote?

The short answer is: you cannot pull a specific commit from a remote. However, you may fetch new data from the remote and then use git-checkout COMMIT_ID to view the code at the COMMIT_ID .


1 Answers

You need to git rebase -i your branch first, in order to make commit2 the first commit after origin/yourBranch.

 x--x--C1--C2--C3 (B)
    |
  (origin/B)

git rebase -i C1~

 x--x--C2'--C1'--C3' (B)
    |
  (origin/B)

Then you can push that commit.

See "git - pushing specific commit":

git push <remotename> <commit SHA>:<remotebranchname>
# Example
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master

It does push all commits up to and including the commit you choose.
But since your commit is the first one, it only pushes that commit.


I would not recommend cherry-picking, as it changes the SHA1 of the commits pushed: when you will eventually push your full branch, you will end up with duplicate commits.

like image 179
VonC Avatar answered Sep 20 '22 15:09

VonC