0

I want to copy my last commit and i want to make a new commit from the copied commit (last commit) to push to the remote branch.

I tried this code:

git rm -r .
git checkout HEAD~0 .
git commit -m "My new Commit"

But it did not work. any suggestions?

Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
Namatullah Shahbazi
  • 236
  • 1
  • 8
  • 21
  • Possible duplicate of [Pushing empty commits to remote](http://stackoverflow.com/questions/20138640/pushing-empty-commits-to-remote) –  Jun 21 '16 at 04:39
  • Do you want to copy your last commit and make changes to it to create the new commit, or do you want to make an exact copy of your last commit with no changes? –  Jun 21 '16 at 05:11
  • What exactly is your usecase? Do you want to have a different comment? – MikeMB Jun 21 '16 at 06:13

5 Answers5

4

If your history is C-B-A (where A is the latest), something has to change for you to make a new commit on top of A. You can bypass this using the --allow-empty flag and just do git commit --allow-empty and you'll get an empty commit on top of A but the commit will be different (since the parent, time etc. are different).

Why do you want to do this?

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
2

If you just want to copy the last commit and play it as a new commit in other branch, try git cherry-pick

spk081
  • 46
  • 5
1

Correct me if I'm wrong but I sounds to me like you're trying to amend your last commit ? e.g. make some corrective changes to the last commit and push it out anew.

You can do this with --amend flag, this will update the latest commit on your current branch. You can then push out that amended commit by doing git push --force or git push --force-with-lease ( introduced in git 1.8.5 ) to make sure you don't overwrite any changes that where pushed out while you where amending your commit.

The force push is needed because by amending the latest commit, you are actually replacing it all together, as such you'll need to force push to explicitly declare you want to get rid of the previous commit.

example:

git commit --amend -a
git push --force-with-lease
Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
1

If your purpose is to make changes to the existing commit before pushing,

  1. Do the need full changes locally and again commit the changes

  2. This will list two commits in your git log.

  3. Merge those two commits to a single commit and push.

To merge the two commits, follow the below steps

Say your history is

$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a

That is, a was the first commit, then b, and finally c.

Running git rebase --interactive HEAD~2 gives you an editor with

pick b76d157 b
pick a931ac7 c

# Rebase df23917..a931ac7 onto df23917
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Changing b's pick to squash will result in the error you saw, but if instead you squash c into b by changing the text to

pick b76d157 b
s a931ac7 c
and save-quitting your editor, you'll get another editor whose contents are

# This is a combination of 2 commits.
# The first commit's message is:

b

# This is the 2nd commit message:

c
When you save and quit, the contents of the edited file become commit message of the new combined commit:

$ git log --pretty=oneline
18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c
df239176e1a2ffac927d8b496ea00d5488481db5 a 
ckruczek
  • 2,361
  • 2
  • 20
  • 23
Twinkle
  • 514
  • 3
  • 8
0

You can use git log to copy your last commit.

git log --oneline -n 1

You usually run git push origin <branch name> to push your local changes to your remote repository.

msc
  • 33,420
  • 29
  • 119
  • 214