Reputation: 3264
Is there a possibility to revert a committed file in Git? I've pushed a commit to GitHub and then I realized that there's a file which I didn't want to be pushed (I haven't finished the changes).
Upvotes: 242
Views: 497398
Reputation: 4743
Use BFG Repo-Cleaner https://rtyley.github.io/bfg-repo-cleaner/ to remove files or folders based on name or filesize.
You will still have to be able to git push -f
which is not generally allowed and should be temporary enabled in repo settings.
Upvotes: 0
Reputation: 39
To remove a directory and all its contents from your Git repository (while keeping it on your local file system), you should use the command with the -r option:
git rm -r --cached /path/to/file
Upvotes: 1
Reputation: 31
This is what I do in such case (simple)
Upvotes: 1
Reputation: 27
I would do the following, say if the branch from which you have made you branch is 'develop'
Do :
git checkout develop /path/to/file
You can find path to file by making a small change in that file and do git status
Upvotes: -2
Reputation: 99
I followed these steps [it always happens to me especially with the node modules folder]
1- git log
2- git revert 04409a858709c82fbf9eae7559d6ae9c34b6fbe6
change the number with your committed one
<it will be long numbers like this 04409a858709c82fbf9eae7559d6ae9c34b6fbe6>
3-git push
This worked for me, but keep in mind that, there is a trick here your first commit will be the last one in the list so check the dates and the commits message.
check this image from my terminal
Upvotes: 0
Reputation: 2185
If you want to remove the file from the remote repo, first remove it from your project with --cache option and then push it:
git rm --cached /path/to/file
git commit -am "Remove file"
git push
(This works even if the file was added to the remote repo some commits ago) Remember to add to .gitignore the file extensions that you don't want to push.
Upvotes: 206
Reputation: 651
You can use the following workflow:
git reset --soft HEAD~1
git reset HEAD /path/to/file
Upvotes: 9
Reputation: 196
Use
git rm --cached /path/to/file
then add, commit and push. Just like above answer, but you need to use --cached
not --cache
.
Upvotes: 5
Reputation: 59
Get the hash code of last commit.
git log
git revert <hash_code_from_git_log>
git push
check out in the GHR. you might get what ever you need, hope you this is useful
Upvotes: 5
Reputation: 5578
update: added safer method
check out the previous (unchanged) state of your file; notice the double dash
git checkout HEAD^ -- /path/to/file
commit it:
git commit -am "revert changes on this file, not finished with it yet"
push it, no force needed:
git push
get back to your unfinished work, again do (3 times arrow up):
git checkout HEAD^ -- /path/to/file
To modify the last commit of the repository HEAD, obfuscating your accidentally pushed work, while potentially running into a conflict with your colleague who may have pulled it already, and who will grow grey hair and lose lots of time trying to reconcile his local branch head with the central one:
To remove file change from last commit:
to revert the file to the state before the last commit, do:
git checkout HEAD^ /path/to/file
to update the last commit with the reverted file, do:
git commit --amend
to push the updated commit to the repo, do:
git push -f
Really, consider using the preferred method mentioned before.
Upvotes: 288
Reputation: 27295
You can revert only one file to a specified revision.
First you can check on which commits the file was changed.
git log path/to/file.txt
Then you can checkout the file with the revision number.
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /path/to/file.txt
After that you can commit and push it again.
Upvotes: 68
Reputation: 691715
Reset the file in a correct state, commit, and push again.
If you're sure nobody else has fetched your changes yet, you can use --amend
when committing, to modify your previous commit (i.e. rewrite history), and then push. I think you'll have to use the -f
option when pushing, to force the push, though.
Upvotes: 5