Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe git diff to git checkout

Tags:

git

If I delete a file, I can revert it in git with:

git checkout filename

If I want to revert all deleted files, I can list them with:

git diff --diff-filter=D --name-only

What I then want to do is restore them, but

git diff --diff-filter=D --name-only | git checkout

Doesn't work, it only repeats the list to stdout, and git checkout seems to receive no input. Ditto for | git checkout HEAD -- and so on.

I've tried this in Windows Command Prompt, in Powershell, and in Git Bash, with the same result each time.

How do I correctly pipe input to git checkout?

like image 622
Roderick Avatar asked Feb 12 '16 12:02

Roderick


People also ask

What is checkout to in git?

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.

Which is correct about git checkout?

In Git, the term checkout is used for the act of switching between different versions of a target entity. The git checkout command is used to switch between branches in a repository. Be careful with your staged files and commits when switching between branches.

What is the impact of git checkout?

git Checkout: The git checkout is navigator command that helps to switch branches. This option prepares for you to work on a particular working branch. It Updates files in the working tree to match the version in the index or the specified tree.

What is the difference between the git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.


2 Answers

You cannot use a pipe for this. You can however use Unix's xargs:

git diff --diff-filter=D --name-only | xargs git checkout

xargs is a tool that reads from stdin and puts the lines as arguments next to its own arguments and calls the result. So if git diff generates a line a and a line b, xargs git checkout will - at least conceptually - generate the line git checkout a b and call this as a command.

like image 68
Willem Van Onsem Avatar answered Sep 28 '22 00:09

Willem Van Onsem


A pipeline results in the standard output of the command to the left of the pipe being used as the standard input of the command to the right of the pipe. The pipeline git checkout | git diff --diff-filter=D --name-only results in the standard output of the git checkout command being used as the standard input of the git diff --diff-filter=D --name-only command. The git diff command does not use standard input, so the git checkout output is silently ignored and you end up with the output of the git diff command.

Instead, try something like git ls-files --deleted -z | xargs -0 git checkout HEAD --. This uses the null character (\0) to delimit file names and will result in deleted files being checked out. This will work for any valid file names, including those containing spaces and other special characters.

like image 30
Dan Cruz Avatar answered Sep 28 '22 00:09

Dan Cruz