The following sequence switches to the master branch, reverts the
Makefile to two revisions back, deletes hello.c by mistake, and gets
it back from the index.
$ git switch master
$ git restore --source master~2 Makefile <1>
$ rm -f hello.c
$ git restore hello.c <2>
-
take a file out of another commit
-
restore hello.c from the index
If you want to restore all C source files to match the version in
the index, you can say
Note the quotes around *.c. The file hello.c will also be
restored, even though it is no longer in the working tree, because the
file globbing is used to match entries in the index (not in the
working tree by the shell).
To restore all files in the current directory
or to restore all working tree files with top pathspec magic (see
gitglossary(7))
To restore a file in the index to match the version in HEAD (this is
the same as using git-reset(1))
$ git restore --staged hello.c
or you can restore both the index and the working tree (this is the same
as using git-checkout(1))
$ git restore --source=HEAD --staged --worktree hello.c
or the short form which is more practical but less readable:
$ git restore -s@ -SW hello.c