Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout .(dot) interactive

Tags:

git

I know git checkout . is one of the most dangerous commands in the git universe (rightfully so). Is there a way to make it interactive? Like how the rm command works with -i option.

I am looking for something along the lines of

git checkout . -i
like image 798
sisanared Avatar asked Apr 02 '17 15:04

sisanared


People also ask

What does git checkout actually do?

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.

What is start point in git checkout?

Checkout a New Branch or Reset a Branch to a Start PointIf the BRANCH-NAME branch doesn't exist, Git will create it and start it at START-POINT . If the BRANCH-NAME branch already exists, then Git resets the branch to START-POINT . This is equivalent to running git branch with -f .

Does git checkout commit?

Checkout old commits Since this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

Is git checkout safe?

Fetched content has to be explicitly checked out using the git checkout command. This makes fetching a safe way to review commits before integrating them with your local repository.


1 Answers

git checkout (and git add as well) support the -p option, which will ask you for every hunk, like this:

diff --git a/post.so b/post.so
index b111bdd..ddba6b1 100644
--- a/post.so
+++ b/post.so
@@ -1 +1,3 @@
-`git checkout` (and `git add` as well) support the `-p` option.
+`git checkout` (and `git add` as well) support the `-p` option, which will ask you for every hunk, like this:
+
+foo bar
Discard this hunk from worktree [y,n,q,a,d,/,e,?]? 

For every hunk you can answer:

y - discard this hunk from worktree
n - do not discard this hunk from worktree
q - quit; do not discard this hunk or any of the remaining ones
a - discard this hunk and all later hunks in the file
d - do not discard this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
like image 195
phihag Avatar answered Sep 28 '22 06:09

phihag