Remove 1 file from commit, keep changes locally.

I went ahead and did a commit of multiple files, my intentions of course to push later.

One of these files I can't send to the server, so I want to remove it from my commit BUT I want to keep the changes I have in this file.. So I want to remove the commit but keep this version in my working copy..

Does this make sense? I'm not sure what to do and I just had a big scare and thought I had lost all my work :/

2 answers

Easiest way assuming you haven't pushed yet and the file was a brand new file that was added in your last commit:

  1. back up your entire local working copy in case I'm wrong...
  2. go to "Working Copy" view and "show all" files
  3. right click on the file and select "Stop Tracking", you will now see the file appear as an untracked file in your working tree and a deleted file in your staging area
  4. press the commit button and select "amend latest commit" and go ahead and commit this change - (this step relies on the fact that the commit where you added the file was the last commit)

Now your commit history shouldn't contain the file but it should still be sitting in your working copy intact and waiting to be committed as an untracked file.

Sumali's answer is great. In case his assumption that the file is "new" is incorrect, you can do something very similar (Full credit to Sumali for steps 1 and 4, which I copied verbatim):

  1. back up your entire local working copy
  2. view the commit in the log view
  3. click the file in the changed files list, then click the "Reverse File" button at the top of the diff
  4. press the commit button and select "amend latest commit" and go ahead and commit this change - (this step relies on the fact that the commit where you added the file was the last commit)

You will have to restore the file in question from your backup.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 23, 2018 in Sourcetree

Tip from the team: configure your repos for hosting goodness!

Supported Platforms macOS Windows We recently introduced support for additional hosting services such as GitHub Enterprise, GitLab (Cloud, Community Edition, Enterprise Edition), and...

841 views 3 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