-1

I have a commit that involves changes to several files. Now I want to remove the changes that I did to one of the files. More precisely, I want this file to be identical to its current version on the remote master. How can I achieve this?

I guess that

git checkout -- <filename>

might do what I need but I am not sure.

I have also seen this syntax:

git checkout <commit> <filename>

I guess that the last command removes the changes of the file introduce by a particular commit but what then the first command does? Does it remove the changes introduced by all commits performed after git pull?

Roman
  • 124,451
  • 167
  • 349
  • 456

1 Answers1

1

Checkout checks out that file in that commit.

When you do git checkout -- filename, that -- just says "end of options", meaning whatever comes after that mark is an argument, not an option.

In this case, the ref HEAD is used if a commit is not specified, so the file as in HEAD is taken and applied to the current commit's file(which means nothing really happens, just the changes that have not been committed disappear; if the directory is clean, nothing happens at all).

If a commit ID is specified, then the file at that commit is applied to the current file.

Example
Suppose you have a commit from 5 days ago that has an ID of 257d2c7.
In this commit you added a verse to a poem you were writing(who says git only helps with source code??) which you then decided is not that good and removed it.
Then you made a couple more commits on top of that. But now you want that particular verse back, how'd you do it?

Like follows:

$ git stash // save existing changes and clean working directory
$ git checkout 257d2c7 my-poem.txt // check out your poem from 5 days ago
$ git stash pop // reapply existing changes and resolve conflicts
$ ... // continue working
cst1992
  • 3,823
  • 1
  • 29
  • 40
  • And what does "checkout" exactly do? I know that it "checks out" but what does it mean? Does it remove the committed changes from the file? – Roman Feb 02 '18 at 08:56
  • Checkout has two forms: one with a ref(such as HEAD, a tag or a branch, etc) and one with a commit. When you specify a ref, it switches to it(eg `git checkout master` will switch you over to the `master` branch). When you specify a bare commit, it'll take the commit and "apply" it to the current branch. I'll add an example to illustrate. – cst1992 Feb 02 '18 at 09:03