I had a bunch of files in the directory in git that weren't tracked. I made the mistake of doing git add .
then committing and pushing the changes. How can I get back to where the files that were untracked before that commit are untracked again, but still in the directory (git reset --hard
deleted all the files, not good for me, instead of untracking them again). I have my many files in different locations that need to be untracked, so git reset <FILE>
won't work for me.
Asked
Active
Viewed 2,675 times
2

theEpsilon
- 1,800
- 17
- 30
-
Possible duplicate of [How to undo 'git add' before commit?](http://stackoverflow.com/questions/348170/how-to-undo-git-add-before-commit) – jjst Oct 20 '16 at 19:26
-
1@jjst please see edits why mine is different. Plus, mine was after the commit, not before – theEpsilon Oct 20 '16 at 19:28
-
@theEpsilon Are you the only user of the repo? You essentially need to [undo a `git push`](http://stackoverflow.com/questions/1270514/undoing-a-git-push). – legends2k Oct 20 '16 at 19:29
-
`git revert
` this will do a new commit with the old commit, works for you? – cpatricio Oct 20 '16 at 19:29 -
@legends2k no, theres many other users – theEpsilon Oct 20 '16 at 19:33
2 Answers
4
First, you should revert the last commit and return all the files back to the staging area by :
git reset --soft HEAD~1
This will not delete any files and it's safe.
Then, you can remove all the files that are added to the staging area by :
git reset
Now if you run the git status
, you can see that all the files are unstaged.
You can then git add
the only files you want.

Farhad
- 12,178
- 5
- 32
- 60
1
$ git commit -m "Something terribly misguided" (1)
$ git reset HEAD~ (2)
<< edit files as necessary >> (3)
$ git add ... (4)
$ git commit -c ORIG_HEAD
- This is what you want to undo
- This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ but leaves your existing changes staged.
- Make corrections to working tree files.
- git add anything that you want to include in your new commit.
- Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Community
- 1
- 1

Moonstruck
- 503
- 4
- 10