How to make GIT ignore my changes

Git

Git Problem Overview


Although the title seems similar to previous questions, I could not find the solution to to my Simple case:

  • I committed to my branch 'ABCD' : git commit -a .....
  • Then decided to visit a previous commit so : git checkout 2387687326487
  • Here I made some changes which was for testing purpose only, and I don't want to keep them
  • I am done, I want to get back to my latest commit(again:ignoring changes whcih i made to the old commit) : the command git checkout 'ABCD' gives error:

> Your local changes to the following files will be overwritten by checkout....please commit or stash...

while I don't want to commit or stash or whatever. I just want to go back home :) what should I do please?

Git Solutions


Solution 1 - Git

Using just

git checkout .

will discard any uncommitted changes in the current directory (the dot means current directory).

EDIT in response to @GokulNK:

If you want to keep those changes for future use, you have 2 options:

  • git stash: this will save those changes in a stack. You can apply the changes to your working copy later using git stash pop
  • git diff > changes.patch: This will save those changes to the file changes.patch. If you want to apply them to your working copy you can do so: git apply changes.patch.

Solution 2 - Git

If you are sure that you don't want to keep your changes, the following line will do :

git checkout -f 'ABCD'

Option '-f' stands for force.

Solution 3 - Git

To undo local changes and go back to the state from the commit you can:

git reset --hard

Solution 4 - Git

Apart from the mentioned solutions you can stash the changes

git stash

Checkout your branch

git checkout 'ABCD'

And eventually, when you are sure that you don't need anything from the stashed changes, and have checked twice with gitk or gitg, throw away the stash:

git stash drop

That is my preferred way, as it is safer.

Solution 5 - Git

If anyone wants to ignore changes on git but keep them on local you can use :

$ git update-index --assume-unchanged <filepath>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrahmanView Question on Stackoverflow
Solution 1 - GitCarlos CampderrósView Answer on Stackoverflow
Solution 2 - GitNicolas BarbeyView Answer on Stackoverflow
Solution 3 - GitMarcin KozińskiView Answer on Stackoverflow
Solution 4 - GitsteffenView Answer on Stackoverflow
Solution 5 - GitJoxieMedinaView Answer on Stackoverflow