Reputation: 20913
We use Bitbucket as our remote repository.
I clone a remote repository that does not exist locally.
git clone https://bitbucket:8443/scm/of/testgit.git
I delete a file.
del Resources\HTML\testgit_07.07.2024.txt
Command git status
displays the following.
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: Resources/HTML/testgit_07.07.2024.txt
no changes added to commit (use "git add" and/or "git commit -a")
I stage the change.
git rm Resources/HTML/testgit_07.07.2024.txt
Now, git status
shows the following.
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: Resources/HTML/testgit_07.07.2024.txt
Now I commit the change.
git commit -m "Abra_test"
Output of the commit.
[master 141dc0c] Abra_test
1 file changed, 1 deletion(-)
delete mode 100644 Resources/HTML/testgit_07.07.2024.txt
Now I want to undo all the above changes. In other words, I want my local repository to be exactly as it was immediately after the clone and before I deleted the file. What git
commands do I need to perform?
I did search before posting this question, but all the answers did not work when I tried to revert a file deletion.
Upvotes: -1
Views: 55
Reputation: 7130
If you created only one more commit that you wish to discard, you could reset your branch to the previous commit with --hard
. This option resets both the index and the working directory.
git reset --hard HEAD~
If you've made more than one commit, and they're still only in your local repository, you might want to reset your local branch to the same commit pointed by its tracking branch. Like so, your branch will look exactly like after the clone. Assuming that your remote is called origin
, you can try
git reset --hard origin/<your_branch>
Alternatively, you could use git revert
. Although you said it didn't work for you, it could be because you might have supplied the wrong commit to revert, or maybe you didn't supply any but HEAD was pointing to another commit. However, if you want to attempt this command again, try to run the command with the SHA1 of the commit you want to revert.
git revert <id-commit-to-revert>
Keep in mind that with the second approach, the accidental deletion and the reverted commit will both show in the commit history. While with git reset
, you're basically disregarding your last n commits, brining your branch to a previous point.
Upvotes: 1