How do I remove a file from the latest commit?
-
3This link is perfect for your question: http://stackoverflow.com/questions/307828/git-remove-file-accidentally-added-to-the-repository – b3h3m0th Sep 18 '12 at 17:09
-
10have u pushed the commit to server? – Paritosh Singh Sep 18 '12 at 18:26
-
7I just do it using: `git reset filepath` – felipekm Jun 06 '16 at 02:44
37 Answers
I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. This can be done like Paritosh Singh suggested:
git reset --soft HEAD^
or
git reset --soft HEAD~1
Then reset the unwanted files in order to leave them out from the commit (the old way):
git reset HEAD path/to/unwanted_file
Note, that since Git 2.23.0
one can (the new way):
git restore --staged path/to/unwanted_file
Now commit again, you can even re-use the same commit message:
git commit -c ORIG_HEAD
EDIT: The easiest way to do this is to use e.g. git gui
. Just select Commit => Amend Last Commit
and simply uncheck the desired file from the commit and click Commit
.

- 45,029
- 5
- 38
- 50
-
122Thanks for this. It's worth adding that if you have already pushed your earlier (wrong) commit, and now try to `git push` your fix up to your repo, it will complain `Updates were rejected because the tip of your current branch is behind its remote counterpart.`. If you're sure that you want to push them (e.g. it's your fork) then you could use the `-f` option to force the push, e.g. `git push origin master -f`. (Do not do this to an upstream repo that others are fetching from) – andy magoon Apr 09 '13 at 19:47
-
@MITjanitor first of all, regardless of that the OP was looking for, *this* is the correct response. Secondly, besides the accepted answer I see 2 other replies with 7 and 2 votes higher than this one, why that happens I don't know. – Pablo Fernandez Aug 13 '13 at 21:46
-
5@PabloFernandez at the top of all of the answers are three tabs which allow you to control the ordering of answers: **active**, **oldest** and **votes**. My guess is yours is set to **oldest**. Switch it to **votes** even though the accepted answer will still be at the top, this answer will be second. – ahsteele Aug 24 '13 at 17:52
-
1Is there any way to reset soft just one file from a commit? That seem to be a very useful option to have. – Punit Soni Sep 18 '13 at 03:21
-
20I knew this much about `git reset` but wanted a way to affect the existing commit "in place". I just learned about `git commit -C`. So for me, what I want is your exact recipe with one more step, the "new commit again" spelled out as `git commit -C [hash of original HEAD commit from first step]`. – metamatt Jan 08 '14 at 23:56
-
1As far as I see it, this will make the commit message lost, unfortunately. Is there a way to preserve the commit msg? – Adrian Föder Apr 17 '14 at 12:42
-
Note that if the files you remove from the commit resulted from a "git mv", you have to do a tiny bit more work to restage the renamed files. – jma Apr 23 '15 at 10:53
-
1@metamatt you can also use `git commit -C ORIG_HEAD`. `ORIG_HEAD` points to the hash just before running `git reset --soft HEAD~1` – reynman May 19 '15 at 17:46
-
1What if this is the initial commit? The common case that you have created a new repo and then used add . to add the tree. Upon inspection you see a file that you didn't ignore, add it to .gitignore and now you want to remove it from the commit before making the initial commit. There is really no reason for this to be as complicated as it is. – Rob_vH Jun 19 '15 at 15:58
-
4Remember to do an actual commit, not an amend. If you do, it will be amending the previous commit. – oops – HexInteractive Jul 22 '15 at 19:05
-
1This is absurd. How come you can't just uncommit the committed files with one command instead of undoing an entire commit? – user124384 Aug 29 '15 at 01:03
-
My initial commit was the one that added the large file. How do I remove the large file without a previous commit to revert to? – Chris Sep 13 '15 at 16:30
-
I've found that the answer I left more accurately addresses this problem, especially in my experience of encountering this. http://stackoverflow.com/questions/12481639/remove-files-from-git-commit/27340569#27340569 – ThatsAMorais Feb 28 '17 at 01:31
-
1If it's the last commit, that is being considered, you can do: `git reset HEAD~ path/to/file && git commit -v --amend`. – x-yuri Jul 29 '17 at 13:21
-
1Will this not work if the commit you want to undo is the very first one starting a new repo? I got `fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.` – hippietrail Aug 15 '17 at 01:07
-
I accidentally pushed to github, I ran the steps above, and now the wrong commit is gone from history but still accessible via the sha1 hash on github. How do I remove it? – Henry Aug 24 '17 at 11:26
-
In my case after the above procedure I had to craate a stash: git stash push -m "Crypto debug" before pushing or in the next git review I would have the same problem. – Zioalex Mar 07 '18 at 09:54
-
Almost worked for me. I only had to run `git run -f` to actually make the changes visible, like @andymagoon said. Apart from that, it worked. – Derk Jan Speelman Aug 26 '19 at 13:47
-
This message in git commit --amend screen saves me trip to stackoverflow: "# Changes to be committed: # (use "git reset HEAD^1
..." to unstage)". – John Jiang Nov 26 '19 at 22:20 -
This answer seems to only apply if the file to be removed is in the most recent commit. What if I want to remove a file in a commit many commits ago? – Geremia Sep 03 '20 at 23:31
-
In zsh (all mac users) you may need to use single quotes git reset 'HEAD^' – joar Feb 15 '21 at 23:47
-
@Geremia See [Brian's answer below](https://stackoverflow.com/a/28173964/851885) if you want to remove files from an earlier commit. – BadHorsie Apr 15 '21 at 16:26
-
Note: If you `git push origin master -f` and get `remote: error: denying non-fast-forward refs/heads/master (you should pull first)`, check here: [how-to-force-push-a-reset-to-remote-repository](https://stackoverflow.com/questions/10544139/how-to-force-push-a-reset-to-remote-repository) – Dave May 26 '21 at 16:38
-
`git restore --staged
` is now a dedicated UI for the step 2. (instead of `reset`) – bloody Jan 13 '22 at 12:53
ATTENTION! If you only want to remove a file from your previous commit, and keep it on disk, read juzzlin's answer just above.
If this is your last commit and you want to completely delete the file from your local and the remote repository, you can:
- remove the file
git rm <file>
- commit with amend flag:
git commit --amend
The amend flag tells git to commit again, but "merge" (not in the sense of merging two branches) this commit with the last commit.
As stated in the comments, using git rm
here is like using the rm
command itself!

- 86,532
- 28
- 194
- 218
-
162
-
19Warning to those browsing this answer: make sure you want to DELETE the file (as in gone gone gone!), not just remove it from the Commit list. – SMBiggs Jun 01 '15 at 14:11
-
11To add to what others say (and to make it easier to remember not to do this unless you really want to): **The `rm` in the `git` command _is_ doing what `rm` itself does!** – yo' Feb 22 '16 at 09:41
-
@CharlesB Can you please add the note from Arkadiy Kukarkin's comment to your answer to give it more visibility? – mopo922 Aug 25 '16 at 15:20
-
3Note that the files can still be restored, in case you changed your mind, the commit before `git commit --amend` is still there and can be found for example with `git reflog`. So it is not as bad as the other comments suggest. – Steohan Dec 06 '17 at 15:18
-
3When pushing you will need force push: `git push -f` if you use `--amend`. – Velizar Andreev Kitanov Oct 15 '20 at 08:44
-
As the accepted answer indicates, you can do this by resetting the entire commit. But this is a rather heavy handed approach.
A cleaner way to do this would be to keep the commit, and simply remove the changed files from it.
git reset HEAD^ -- path/to/file
git commit --amend --no-edit
The git reset
will take the file as it was in the previous commit, and stage it in the index. The file in the working directory is untouched.
The git commit
will then commit and squash the index into the current commit.
This essentially takes the version of the file that was in the previous commit and adds it to the current commit. This results in no net change, and so the file is effectively removed from the commit.

- 6,882
- 3
- 33
- 31
-
15This is a much better answer for removing just a single file, without overhauling the entire commit. – nimish May 30 '17 at 17:16
-
This is exactly the same as this answer :D https://stackoverflow.com/a/27340569/1623984 – ThatsAMorais Jan 22 '18 at 16:55
-
@ThatsAMorais indeed :-). I'm wondering if this question got merged with another, and thats why I didn't see it. Or maybe I'm just blind. Either case, I think I'm inclined to leave it as based on votes it seems to be more popular (maybe people prefer the short and direct answer). – phemmer Jan 22 '18 at 20:08
-
By all means leave it! :) We clearly had the right idea, and the purpose is to help. I think merging is a good theory. – ThatsAMorais Jan 22 '18 at 22:44
-
How do you do this if you already pushed to a branch on github? I tried to push after doing this fix and got the message "Updates were rejected because the tip of your current branch is behind hint: its remote counterpart." – Ollie Williams Mar 16 '18 at 11:26
-
-
1Nice answer, but there is one gotcha you may need to watch out for: if you have any added or deleted files currently staged they will be added to the commit via the `git commit --amend --no-edit` command. Not sure why this is but it just bit me. – krayzk Dec 07 '18 at 16:50
-
Love it. Note, for anyone with zsh it might have to be `git reset 'HEAD^' -- ....` with quotes – johansenja Feb 07 '20 at 13:10
-
Instead of `HEAD^`, put the commit ID of the commit before you first committed the file. – Andrew Jul 08 '20 at 05:16
-
But this answer is narrow case-biased (only if wholly new file was added in last commit). My case is to remove the file (object) form the last commit, period (its changes), the file had been tracked before and so is to stay. – bloody Jan 13 '22 at 12:41
-
When I do this, I get my original change (the one I don't want anymore) in the unstaged area and the one I want in the staged area (back to the original file before I changed it). To just get the change, I run a `git restore .` which gets rid of the unstaged changes and leaves me with the ones I want in the staged area. – codingjeremy May 24 '23 at 22:56
Existing answers are all talking about removing the unwanted files from the last commit.
If you want to remove unwanted files from an old commit (even pushed) and don't want to create a new commit, which is unnecessary, because of the action:
- Find the commit that you want the file to conform to using ;
git log --graph --decorate --oneline
- Checkout that commit using
git checkout <commit_id> <path_to_file>
you can do this multiple times if you want to remove many files.
3.
git commit -am "remove unwanted files"
4.
Find the commit_id of the commit on which the files were added mistakenly, let's say "35c23c2" here
git rebase 35c23c2~1 -i // notice: "~1" is necessary
This command opens the editor according to your settings. The default one is vim. If you want to change the global git editor, use;
git config --global core.editor <editor_name>
5.
Move the last commit, which should be "remove unwanted files", to the next line of the incorrect commit("35c23c2" in our case), and set the command as fixup
:
pick 35c23c2 the first commit
fixup 0d78b28 remove unwanted files
You should be good after saving the file.
6.
To finish :
git push -f
If you unfortunately get conflicts, you have to solve them manually.

- 213
- 1
- 5
- 11

- 30,156
- 15
- 86
- 87
-
12If you want to actually remove the files from the repo (not the filesystem), rather than merely revert them to a previous version, then instead of step 1 do `git rm --cached
`. – waldyrious Oct 21 '16 at 12:34 -
4Wait you can just move commits around the interactive-rebase file at will? – Dan Rosenstark May 25 '17 at 18:50
-
4
-
13This process could be made a bit easier by adding `--fixup=35c23c2` to the `git commit` command. This will setup the commit automatically as a fixup of the required commit and so you won't need to specify it in the rebase. Additionally, if you add `--autosquash` to the `git rebase` command, git will automatically move your commit to the correct location, so you don't need to do anything in the interactive rebase - just save the result (which means you don't even need to `-i` flag, though I like to use it anyway to make sure everything looks as I expect). – Guss Jun 21 '17 at 15:06
-
Maybe since commit -a is what got him into trouble in the first place, teach the explicit add and commit instead? :) – Chad Stewart Mar 21 '18 at 17:59
-
1I'd avoid doing the last step (`git push -f`) unless you **really** know what you are doing with that command. At the very least, do `git push --force-with-lease`, which is safer. See https://developer.atlassian.com/blog/2015/04/force-with-lease/ for more info. – mc_kaiser Jun 04 '18 at 17:23
-
This worked great for me on my local branch, but when I tried to push it back to the remote repo, GIT gave en error: "Updates were rejected because the tip of your current branch is behind". My branch was not behind though and doing a pull just put the original file back! :( – ccbunney Sep 19 '18 at 13:59
-
3didnt work, it just does nothing after doing checkout spcific commit and file – Habib Rehman Mar 28 '19 at 12:10
-
Let's suppose I have commit 'a' and files a1,a2,a3 and commit 'b' with files b1,b2 and commit c with files c1,c2,c3 and commit 'c' being the latest. How do I delete only file a2 from commit 'a' without disturbing anything from commits b and c. – Jp Reddy Mar 10 '20 at 09:09
If you have not pushed the changes on the server you can use
git reset --soft HEAD~1
It will reset all the changes and revert to one commit back
If you have pushed your changes then follow steps as answered by @CharlesB

- 6,288
- 5
- 37
- 56
-
2-1 git reset removes changes happened file from staging area, here the change has been committed – CharlesB Sep 18 '12 at 18:38
-
OK, but I'll keep my downvote since it's not what the OP want :) sorry – CharlesB Sep 18 '12 at 18:44
-
ok its fine for me, but why opponent don't want this. What is the problem? – Paritosh Singh Sep 18 '12 at 19:23
-
not a big deal, but because the OP wants to remove unwanted file from last commit, and it just resets to the state before committing. still you have to redo the commit. – CharlesB Sep 18 '12 at 19:46
-
in myour case also he has to redo commits, only difference i told is one more method and i don't think there is any problem in doing this if commit was local, rest up to you :). – Paritosh Singh Sep 18 '12 at 20:04
-
-
3
Removing the file using rm will delete it!
You're always adding to a commit in git rather than removing, so in this instance return the file to the state it was in prior to the first commit (this may be a delete 'rm' action if the file is new) and then re-commit and the file will go.
To return the file to some previous state:
git checkout <commit_id> <path_to_file>
or to return it to the state at the remote HEAD:
git checkout origin/master <path_to_file>
then amend the commit and you should find the file has disappeared from the list (and not deleted from your disk!)

- 1,204
- 9
- 8
git checkout HEAD~ path/to/file
git commit --amend

- 499
- 4
- 3
-
1This is the best way to modify the last commit, the one that has not been pushed. This will reset changes in one file, effectively removing that file from the last commit. – Alex Bravo Aug 17 '16 at 16:07
-
2This also changed the file. How to preserve local changes in the file? – theonlygusti Feb 21 '20 at 20:48
I will explain to you with example.
Let A, B, C be 3 successive commits. Commit B contains a file that should not have been committed.
git log # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>

- 1,083
- 10
- 26

- 1,035
- 1
- 10
- 16
-
1If the particular file is not in the last or previous commit, this is the most elegant way. I'd even say it's the most elegant way in general. I like interactive rebasing. – bvgheluwe Aug 07 '19 at 07:20
-
For a single commit, change `noop` to `edit [A_commit_ID]` or `e [A_commit_ID]` – TamusJRoyce Feb 19 '20 at 18:53
-
The following will unstage just the file you intended, which is what the OP asked.
git reset HEAD^ /path/to/file
You'll see something like the following...
Changes to be committed: (use "git reset HEAD ..." to unstage)
modified: /path/to/file
Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)
modified: /path/to/file
- "Changes to be committed" is the previous version of the file before the commit. This will look like a deletion if the file never existed. If you commit this change, there will be a revision that reverts the change to the file in your branch.
- "Changes not staged for commit" is the change you committed, and the current state of the file
At this point, you can do whatever you like to the file, such as resetting to a different version.
When you're ready to commit:
git commit --amend -a
or (if you've got some other changes going on that you don't want to commit, yet)
git commit add /path/to/file
git commit --amend

- 659
- 8
- 16
-
3juzzlin's answer is great, but its excessive to unstage the entire commit when you only wish to unstage one. Unstaging the whole commit can cause problems if you have currently unstaged changes on files in that commit that you do not want to lose. – ThatsAMorais Dec 07 '14 at 07:16
You can simply try.
git reset --soft HEAD~1
and create a new commit.
However, there is an awesome software "gitkraken". which makes it easy to work with git.

- 1,660
- 13
- 13
-
1And just to note: after this, you'd do `git commit --amend` to have the file removal updated in your last commit; and afterwards, you can check it has indeed been removed with `git log -1 --stat` – sdbbs Nov 13 '19 at 14:12
git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit -m "removed unwanted file from git"
will leave you the local file still. If you don't want the file locally either, you can skip the --cached option.
If all work is on your local branch, you need to keep the file in a later commit, and like having a clean history, I think a simpler way to do this could be:
git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit --squash <commit_id>
git add <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit -m "brand new file!"
git rebase --interactive <commit_id>^
and you can then finish the rebase with ease without having to remember more complex commands or commit message or type as much.

- 1,201
- 11
- 9
-
This works for me. I assume if you want to add the files back into the mix, just use git add -A or git add . and they are back. – Alexander Mills May 16 '16 at 22:14
-
This worked for me. I have no idea why `git restore --staged` didn't work on my version `2.37.3.windows.1`. I was trying to remove `.idea` folder that had some files in it – BrunoElo Oct 09 '22 at 15:06
Using git GUI can simplify removing a file from the prior commit.
Assuming that this isn't a shared branch and you don't mind rewriting history, then run:
git gui citool --amend
You can un-check the file that was mistakenly committed and then click "Commit".
The file is removed from the commit, but will be kept on disk. So if you un-checked the file after mistakenly adding it, it will show in your untracked files list (and if you un-checked the file after mistakenly modifying it it will show in your changes not staged for commit list).

- 12,022
- 5
- 54
- 65
-
3On Ubuntu, you can install git gui with `sudo apt-get install git-gui` – JDiMatteo Jun 15 '17 at 16:15
-
Thank you! I was stuck with a problem (bug?) where a folder containing a .git repo was added, and all the regular remove commands didn't work. This however helped. It being a few commits back, I first utilized `git rebase -i HEAD~4` and then ran your command to open the editor. Another note: "Unstaging" can be found in the "Commit" menu. – Johny Skovdal Jun 12 '18 at 10:13
-
The easiest solution of all. Simplest to remember. And much less error-prone than using `git reset --soft HEAD^` (remembering the --soft arg), followed by `git commit -c ORIG_HEAD` (rather than --amend, which screws everything up). – Brent Faust Sep 15 '18 at 05:51
-
thanks, there were 3 binary objects in my last commit and push was taking forever. Your answer helped me to delete 3 objects. – user1154390 Nov 12 '20 at 10:41
Keep the file locally (remove from git commit)
If you want to keep the file you can use --cached
like so:
git rm --cached filename
Delete local file (and remove from git commit)
else if you want to delete the file just use:
git rm filename

- 17,345
- 6
- 90
- 86
If you want to preserve your commit (maybe you already spent some time writing a detailed commit message and don't want to lose it), and you only want to remove the file from the commit, but not from the repository entirely:
git checkout origin/<remote-branch> <filename>
git commit --amend

- 6,456
- 3
- 36
- 47
Do a sequence of the following commands:
//to remove the last commit, but preserve changes
git reset --soft HEAD~1
//to remove unneded file from the staging area
git reset HEAD `<your file>`
//finally make a new commit
git commit -m 'Your message'

- 17,953
- 10
- 93
- 108

- 6,943
- 4
- 44
- 51
-
I've tried to do these steps, and I see this error error: failed to push some refs to 'git....' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details. (after git pull I have the same changes) – Dezigo Sep 11 '15 at 09:06
-
It means that the state of your remote repo has changed while you were doing your local job. And after 'git pull' your local changes should be merged with remote ones, that's it. Of course, your changes must remain. – Sergey Onishchenko Sep 11 '15 at 09:36
-
In other words, if you get that error @Dezigo, add the -f flag to force the update. – jungledev Jan 12 '16 at 20:18
Just wanted to complement the top answer as I had to run an extra command:
git reset --soft HEAD^
git checkout origin/master <filepath>
Cheers!

- 71
- 1
- 2
-
Welcome. This answer would be better if you explained what the commands actually do. – Mark Chorley May 25 '16 at 15:50
If you want to remove files from previous commits use filters
git filter-branch --prune-empty --index-filter 'git rm --ignore-unmatch --cached "file_to_be_removed.dmg"'
If you see this error:
Cannot create a new backup. A previous backup already exists in refs/original/ Force overwriting the backup with -f
Just remove refs backups on your local repo
$ rm -rf .git/refs/original/refs

- 520
- 6
- 14
-
Note that in case of existing backup, the error message clearly indicates the solution: add "-f" as in `git filter-branch -f [...etc...]`. No need to manually remove the refs. – ralfoide Aug 29 '20 at 07:36
git reset --soft HEAD~1.
This will undo the last commit in local repos and move everything back to stage area like before doing the commit. Then just use any Git UI tool as normal (like TortoiseGit, Git UI, Git Extensions...) to unstage the files that we do not want to commit then do the commit again.

- 2,106
- 1
- 28
- 34
git reset --soft HEAD~1
git reset HEAD /file/to/delete
git rm --cached /file/to/delete
git commit --amend -m "your message"
- key point is the --amend
then can:
git push -u origin master

- 12,947
- 1
- 71
- 56
Something that worked for me, but still think there should be a better solution:
$ git revert <commit_id>
$ git reset HEAD~1 --hard
Just leave the change you want to discard in the other commit, check others out
$ git commit --amend // or stash and rebase to <commit_id> to amend changes

- 1,285
- 2
- 13
- 20
git reset --soft HEAD^
winds back your commit, and when you type git status
, it tells you what to do:
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

- 6,873
- 11
- 48
- 102
Actually, I think a quicker and easier way is to use git rebase interactive mode.
git rebase -i head~1
(or head~4, how ever far you want to go)
and then, instead of 'pick', use 'edit'. I did not realize how powerful 'edit' is.
https://www.youtube.com/watch?v=2dQosJaLN18
Hope you will find it helpful.

- 17,953
- 10
- 93
- 108

- 445
- 4
- 3
Had the same issue where I have changes in a local branch where I wanted to revert just one file. What worked for me was -
(feature/target_branch below is where I have all my changes including those I wanted to undo for a specific file)
(origin/feature/target_branch is the remote branch where I want to push my changes to)
(feature/staging is my temporary staging branch where I will be pushing from all my wanted changes excluding the change to that one file)
Create a local branch from my origin/feature/target_branch - called it feature/staging
Merged my working local branch feature/target_branch to the feature/staging branch
Checked out feature/staging then git reset --soft ORIG_HEAD (Now all changes from the feature/staging' will be staged but uncommitted.)
Unstaged the file which I have previously checked in with unnecessary changes
Changed the upstream branch for feature/staging to origin/feature/target_branch
Committed the rest of the staged changes and pushed upstream to my remote origin/feature/target_branch

- 5,518
- 9
- 31
- 50

- 41
- 4
If you dont need that file anymore, you could do
git rm file
git commit --amend
git push origin branch

- 547
- 6
- 18
if you dont push your changes to git yet
git reset --soft HEAD~1
It will reset all the changes and revert to one commit back
If this is the last commit you made and you want to delete the file from local and remote repository try this :
git rm <file>
git commit --amend
or even better :
reset first
git reset --soft HEAD~1
reset the unwanted file
git reset HEAD path/to/unwanted_file
commit again
git commit -c ORIG_HEAD

- 2,498
- 23
- 28
Another approach that we can do is to :
- Delete the file
- Make a new commit with the deleted file
- Do an interactive rebase and squash both commits
- Push

- 854
- 10
- 18
If you're using GitHub and haven't yet pushed the commit, GitHub Desktop solves this problem easily:
- Choose Repository -> Undo Most Recent Commit
- Unselect the file that you mistakenly added. Your previous commit message will be in the dialog box already.
- Press the Commit button!

- 8,084
- 8
- 48
- 62

- 1,888
- 20
- 25
This is worked for me to remove the file from bit bucket repo which I pushed the file to branch initially.
git checkout origin/develop <path-to-file>
git add <path-to-file>
git commit -m "Message"
git push

- 31
- 5
-
I think this is an underrated answer for many use cases. Git checkout of the original file from the main branch might be what the user is after. – josh Sep 23 '20 at 19:57
I copied the current files in a different folder, then get rid of all unpushed changes by:
git reset --hard @{u}
Then copy things back. Commit, Push.

- 1,796
- 4
- 30
- 65
From git 2.23 onwards, you can simply use this command:
git restore --staged <file>
-
1This doesn't achieve anything at all by itself in the context of the question if you haven't `git reset` the last commit beforehand. This command simply un-stages the file, i.e. doesn't modify anything from the commit itself, which is what OP's is asking for. You don't seem to understand the question. – adamency Nov 03 '22 at 10:21
-
1@adamency I agree 100% with you.5years down the lane, I realized my answer is not to the question. But thanks for bringing that up – suulisin Nov 03 '22 at 14:08
If for any reason (as it was in my case) you'll have trouble reseting all files from the last commit - as in git reset --soft HEAD^
- and your case mets the following conditions, you can do as I explain below.
- the file you are trying to remove already existed in repository before the last commit
- you can undo all changes in the file since the previous commit (
HEAD~2
- the commit just before the last commit)
You can then undo all the changes since the previous commit (HEAD~2), save the file, stage it (git add filename
) and then run git commit --amend --no-edit
.
This will commit the "new changes" - which is actually recommitting the file to the last commit as it was before the last commit.

- 1,985
- 2
- 19
- 22
Here is the step to remove files from Git Commit.
>git reset --soft HEAD^1(either commitid ) -- now files moved to the staging area.
>git rm --cached filename(it will removed the file from staging area)
>git commit -m 'meaningfull message'(Now commit the required files)

- 1,591
- 19
- 11
Now that Apple has non-cosensually updated all MacOS users to zsh, the correct answer doesn't work anymore.
git reset --soft HEAD^
zsh: no matches found: HEAD^
You can prevent the shell from treating the ^ as a special character by single quoting it.
git reset --soft 'HEAD^'
Alternatively you can disable this behavior in your shell. by updating your ~/.zshrc with
unsetopt nomatch

- 473
- 3
- 7
The best way is to use a SourceTree (or you can do it from Command Line also, as you want) and:

- 419
- 4
- 11
For those of you who are not as nerdy as all the other answers require, and you like to use the desktop app, this answer is for you.
Using the GitHub Desktop app for Windows, you can click on the History tab. Then right click on the commit you messed up, and select "Undo commit...". This will bring all the files back to your staging area and you can start over.

- 1,741
- 23
- 35
None of the answers at this time are reasonable. Sounds like there is enough demand that a real solution should be proposed: https://github.com/git/git/blob/master/Documentation/SubmittingPatches
git --uncommit <file name>
would be nice. I get that we do not want to modify history, but if I am local and accidentally added local "hack" file and want to remove it from the commit this would be super helpful.

- 953
- 8
- 20