Modify commit

Is there a way to modify an older commit(the files in that commit) without create a new commit?

1 answer

1 accepted

If the commit and at least on later one is already pushed to the server:

The short answer is "no".

The long answer is that it is possible to do this with Git (not SourceTree), but it is rather complicated and all the other users of the repository would find themselves with an incompatible version of the history that they would need to repair afterwards.

The best way to do this is to revert the commit you want to modify and create a new one with the right changes.

 

If the code is not pushed yet, which seems to be the case:

  1. (Stash any uncommitted changes you have.)
  2. Do a soft reset to the last commit before the commit you want to change. (Right-click on the commit you want to reset to and click the right option in the context menu.) Now all the changes made in the commits above this point are converted to uncommitted changes.
  3. Re-create the commit you wanted to edit and all the others after it.
  4. (Re-apply stashed changes and continue working.)

 

The commit is not pushed. Can it change the answer?

Yes, I updated the answer accordingly.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Sourcetree

Tip from the team: configuring Git or Mercurial in Sourcetree

Supported Platforms macOS Windows To make using Sourcetree as simple yet powerful as possible we embed (bundle) dependencies such as Git, Git LFS, and Mercurial. We strive to keep these...

571 views 1 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you