Reputation: 1544
I did a pull of a different branch by mistake and did git reset HEAD@{1}
to undo it. Now when I type git status
I get a huge list of untracked and modified files from the branch that I pulled.
How to remove them from staging so that I do not have to commit them?
Upvotes: 1
Views: 2197
Reputation: 20457
The command you want is
git reset --hard
The --hard
flag tells git to not only move HEAD, but also reset local files to be as they were at that commit.
This will only reset changes on tracked files, if you used --hard
on your initial reset to undo your pull, any new files introduced by the pull would have been removed, but if you've done a soft reset, new files introduced by the pull will still be there, but your HEAD will be a commit where these files are not tracked.
To remove untracked files from the working copy, user git clean
git clean -n
Will do a dry-run and show you what it would remove. Unless you've set it up otherwise, you'll probably need to supply the -f
force flag to make it actually remove the files.
Be careful with git clean, because these files are untracked, there's no way to get back a file you remove by accident.
Upvotes: 3
Reputation: 5837
I would do a hard reset as in @SpoonMeiser's answer, but I wouldn't do a hard reset of the reflog. The reflog is special - we might need to consult it but we shouldn't need to reset to it in everyday git use.
Instead, I would find the last commit of your branch before you pulled, and do:
git reset --hard 46c80011b9845548000f8ed2178755d28c5e6832
or whatever the hash is.
You should be able to find the hash by doing git log
. If the pull performed a merge, the most recent commit will be a merge. The output of git log looks like this:
commit eddb8b70c9167cf88653d7649f054c0d69a5ee8a
Merge: a89faff 980a815
Author: jwg <[email protected]>
Date: Wed Mar 15 10:22:16 2017 +0000
Merge branch 'some_random_remote_branch' of git_server:foo/bar into my_precious_local_branch
The second line shows the two commits which were merged. One of these is the remote branch, one is my_precious_local_branch
, you should be able to find out which.
If the last commit isn't a merge commit, then the pull was a fast-forward. This means that your local branch didn't have anything on it which wasn't on the remote branch. However, you might still want your local branch back. You can go through git log
and decide exactly which commit you want to restore, then do git reset --hard
to it.
Upvotes: 1