How to copy the last commit as a new commit in Git?

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:

  • python script to execute git command and display output on a html page
  • why merge does not cause conflict
  • How could I use a different strategy for just one commit in a rebase?
  • how to know the depth of a git's shallow clone?
  • Git workspace on OSX (case insensitive but preserving) have pulled case-related changes
  • Composer create-project from bitbucket
  • git rm -r .
    git checkout HEAD~0 .
    git commit -m "My new Commit"
    

    But it did not work. any suggestions?

  • Proper git remote tracking practice
  • How can I “clean up” branches locally, without breaking everything for others?
  • In Git, can you retrieve the date when you pulled a certain commit from the repo?
  • Create a git repo using an alias from terminal
  • git from a C# application
  • Pushing using Git from a pc to another without using Github
  • 5 Solutions collect form web for “How to copy the last commit as a new commit in Git?”

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

    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?

    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
    

    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 
    

    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.

    Git Baby is a git and github fan, let's start git clone.