Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-add

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"

like image 900
ben Avatar asked May 23 '15 11:05

ben


People also ask

How do you add an individual file in git?

Add only one file, or one part of the changed file: git add README.md. Commit the first set of changes: git commit -m "update the README to include links to contributing guide" Add another file, or another part of the changed file: git add CONTRIBUTING.md.

How do I add everything to my git status?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed.

How do I add files to a git staging area?

Add files to the staging area by using the "git add" command and passing necessary options. Commit files to the local repository using the "git commit -m <message>" command. Repeat.

Which command is used to add files for tracking in the git repository git status?

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .


1 Answers

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.

like image 76
Nick Volynkin Avatar answered Sep 30 '22 04:09

Nick Volynkin