Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout is not removing files that it should

Using a public repo, I want to get my master branch back to a certain commit from the past. I have reviewed the options and the best thing for me looks to be a simple checkout to the desired commit, then commit to the master branch. However when I do the checkout it does not remove some files that have been added into master after the specified commit hash.

So for example, if I want to get back to commit aaa1:

$ cd working-copy-top-dir
$ git checkout master
$ git checkout -- .
$ git clean -fd
$ git checkout aaa1 .
$ git clean -fd

But at this point some files added after aaa1 are still in the working copy. What is the checkout command to get the working copy data back how it was at aaa1?

$ git --version
git version 2.7.2.windows.1
like image 787
mulllhausen Avatar asked May 13 '16 04:05

mulllhausen


People also ask

Will git checkout remove untracked files?

git checkout does not affect untracked files. Git only manages tracked files, and it works fairly hard to avoid letting you lose data (which is critical).

How do I force git checkout?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

Does git checkout update files?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

How do I permanently delete a file from a git repository?

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.


2 Answers

TL;DR: remove everything first

When you used git checkout aaa1 ., you told Git to translate aaa1 to a commit, find that commit (more precisely, its tree), and copy every file in that commit to your index / staging area and work-tree.

Let's say, just for the sake of argument, that you start with master containing two files, README and hello:

$ git checkout master
[output snipped]
$ ls
README   hello
$ cat README
Yay, you read me!
$ cat hello
world
$ 

Let's say further that commit aaa1 exists and has two files in it, README and addendum. Its README says Thank you for reading. Let's do that checkout:

$ git checkout aaa1 -- .
[output snipped]
$ ls
README    addendum  hello

(I added the --: it's not actually required here, but it's good practice.) The contents of README are the updated README. The file addendum has also been extracted. The file hello is not removed and remains unchanged from the version found in master. The updated README and hello are staged:

$ git status --short
M  README
A  addendum

but hello is not removed:

$ git ls-files --stage
100644 ac6f2cf1acbe1b6f11c7be2288fbae72b982823c 0   README
100644 7ddf1d71e0209a8512fe4862b4689d6ff542bf99 0   addendum
100644 cc628ccd10742baea8241c5924df992b5c019f71 0   hello

Using git clean, even with -x, will have no effect: nothing needs cleaning; there are no unstaged files (hello is staged, it's just not modified).


You specifically wanted to get the work-tree to match commit aaa1, byte for byte. To do that, you must find files that are in the index now, but were not in aaa1, and remove them.

There is, however, an easier way: just remove everything. Then, use your git checkout aaa1 -- . to extract everything from aaa1. This will fill in the index and work-tree from aaa1: any files that need to be restored to the way they were before removing, are restored (to the way they were in aaa1 which is the same as the way they are in HEAD). Any files that need to be changed to match the way they were in aaa1, are restored (to the way they were in aaa1 which is different).

$ git rm -rf .
rm 'README'
rm 'addendum'
rm 'hello'
$ git checkout aaa1 -- .
$ git ls-files --stage
100644 ac6f2cf1acbe1b6f11c7be2288fbae72b982823c 0   README
100644 7ddf1d71e0209a8512fe4862b4689d6ff542bf99 0   addendum
$ git status --short
M  README
A  addendum
D  hello

You can now commit and you will have a new commit on master that, regardless of what was there before, has exactly the same tree as aaa1.

(Whether this is a good idea is another thing entirely, but it will get you the desired state.)

like image 116
torek Avatar answered Sep 30 '22 01:09

torek


Do you want to roll back your repo to that state? Or you just want your local repo to look like that?

See https://git-scm.com/docs/git-reset for git reset.

CASE 1: if you do

git reset --hard [commit hash]

It will make your local code and local history be just like it was at that commit. But then if you wanted to push this to someone else who has the new history, it would fail.

CASE 2: if you do

git reset --soft [commit hash]

It will make your local files changed to be like they were then, but leave your history etc. the same.

I found answer here. Also you can see related answer here.

like image 31
pRaNaY Avatar answered Sep 29 '22 23:09

pRaNaY