This git add-on command lets you tweak/edit files in the index/stage directly without touching the checked out working copy.
- Use case 1: So you accidentally
git-add
ed some changes that you didn't want to merge just yet and would like to partially undo? - Use case 2: You want to make a quick commit to some file(s) but keep the checked out version(s) as-is?
- Use case 3: You have been trying to use
git reset -p
but failed spectacularly? - Use case 4: You want to stage something that is not an interpolation of the checked out and the committed version?
Save the git-tweak-index.sh file in ~/bin/ or /usr/local/bin/ or something else in your PATH.
Rename the file to not contain the .sh
extension.
Finally run chmod +rx git-tweak-index
in chosen "bin" directory.
Make sure you have a text editor of your choice set either through core.editor
in your git config or through the EDITOR
environment variable.
git config --global core.editor <editor>
# or
export EDITOR=<editor>
# or
export GIT_EDITOR=<editor>
git tweak-index myfile.txt
git tweak-index myfile.txt myfile2.txt
$ git init
$ seq 1 10 > foo
$ cat foo
1
2
3
4
5
6
7
8
9
10
$ git add foo
$ git commit -m '1-10'
$ sed -i '/[567]/d' foo
$ git add foo
$ git diff --staged | cat
diff --git foo foo
index f00c965..07c816d 100644
--- foo
+++ foo
@@ -2,9 +2,6 @@
2
3
4
-5
-6
-7
8
9
10
$ echo 42 >> foo
$ git add foo
$ git diff --staged
diff --git foo foo
index f00c965..04a8e20 100644
--- foo
+++ foo
@@ -2,9 +2,7 @@
2
3
4
-5
-6
-7
8
9
10
+42
# oops, I didn't really want to add that 42 to the index (not yet at least)
$ git tweak-index foo
# editor pops up, you remove "42" from the file, save, exit
$ git diff --staged | cat
diff --git foo foo
index f00c965..07c816d 100644
--- foo
+++ foo
@@ -2,9 +2,6 @@
2
3
4
-5
-6
-7
8
9
10
# looks great, 42 is no longer index/stage now, just what I wanted
$ git diff | cat
diff --git foo foo
index 07c816d..04a8e20 100644
--- foo
+++ foo
@@ -5,3 +5,4 @@
8
9
10
+42
# .. but it is still there in my working copy, wonderful!
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: foo
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: foo
$ git commit -m 'remove 5-7'
I was going to call this edit-index but that was already taken, so I picked tweak-index instead. If you want to use some other name for it, just create an alias for it, for example to use edit-index
:
git config --global alias.edit-index tweak-index