Search code examples
gitgit-add

How to add specific files in git by their number in git status?


I often encounter the following scenario:

modified:   assembly/main.debug.s
modified:   ../src/cd/Config.java
modified:   ../src/cd/memoization/cfg/SubgraphFinder.java
modified:   ../src/cd/memoization/cfg/SubgraphMap.java
modified:   ../src/cd/profiler/Profile.java
modified:   ../test/cd/test/TestSamplePrograms.java
modified:   ../../notes/20150521.txt

Here I have a bunch of files and I want to include them in different commits. What I do so far is to do a bunch of git add <pathspec> followed by a respective git commit. The <pathspec> is what annoys me. Is there something like the following?

1 modified:   assembly/main.debug.s
2 modified:   ../src/cd/Config.java
3 modified:   ../src/cd/memoization/cfg/SubgraphFinder.java
4 modified:   ../src/cd/memoization/cfg/SubgraphMap.java
5 modified:   ../src/cd/profiler/Profile.java
6 modified:   ../test/cd/test/TestSamplePrograms.java
7 modified:   ../../notes/20150521.txt

git magic 2,3,5 -m "My super simple commit"


Solution

  • Example repo:

    I'm using an example repo with four files: a, b, c, d.
    Here a is tracked, changed and staged; b is tracked, changed and not staged; c in untracked and staged; d is just untracked.

    enter image description here

    External tool: git-number

    When run without arguments, 'git number' runs 'git status' and attach a unique number for each line of filename printed by 'git status', and it will 'remember' this number-to-filename association. When run with arguments, like this:

    $ git number <any git command> [one or more numbers or git options/args]
    

    'git number' will run that and subtitute all the numbers to their equivalent filenames. Non-numeric argument are passed intact to git.

    enter image description here

    This is available with other commands.

    enter image description here

    External tool: SCM Breeze

    SCM Breeze is a set of shell scripts (for bash and zsh) that enhance your interaction with git. It integrates with your shell to give you numbered file shortcuts, a repository index with tab completion, and many other useful features.

    SCM Breeze utilizes keyboard shortcuts and aliases to work with git files by number:

    Ctrl + x, c => git_add_and_commit - add given files (if any), then commit staged changes

    Ctrl + x, Space => git_commit_all - commit everything

    git add:

    $ ga 1
    

    git diff:

    $ gd 2
    

    git reset:

    $ grs 3
    

    git commit:

    $ gco 4
    

    Native way with git add -i

    git add -i
    

    From Git reference:

    -i
    --interactive
    Add modified contents in the working tree interactively to the index. Optional path arguments may be supplied to limit operation to a subset of the working tree. See “Interactive mode” for details.

    You can remember this as -intuitive, because the interface is really intuitive. Well, at least to hardcore Vim users.

    Opening the interactive mode: enter image description here

    Adding (staging) a tracked file: enter image description here

    Adding an untracked file: enter image description here

    See the changes: enter image description here

    If you're stuck in the middle of adding, hit Return with an empty string.

    Note:

    If you're confused with the appearance and coloring: I've been using iTerm2 + zsh + oh-my-zsh.