add a specific file without typing the entire path out?

Viewed 154

git status

modified:   src/main/java/common/fileOption/FileOption.java
modified:   src/main/java/framework/Main.java
modified:   src/main/java/framework/utils/FileUtils.java

I would like to git add one of the above files, is there a quick way to do so without having to type out the entire path?

I am hoping for a mechanism to reference one of them perhaps by number. Say I want to add the last file only, then maybe something like git add --something 3?

3 Answers

You can use the Interactive Staging Git Tool.

If you run git add with the -i or --interactive option, it opens an interactive shell mode, displaying something like this:

           staged     unstaged path
  1:    unchanged        +0/-3 src/main/java/common/fileOption/FileOption.java
  2:    unchanged        +0/-3 src/main/java/framework/Main.java
  3:    unchanged        +0/-3 src/main/java/framework/utils/FileUtils.java

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> a
  1: src/main/java/common/fileOption/FileOption.java
  2: src/main/java/framework/Main.java
  3: src/main/java/framework/utils/FileUtils.java
Add untracked>> 3
  1: src/main/java/common/fileOption/FileOption.java
  2: src/main/java/framework/Main.java
* 3: src/main/java/framework/utils/FileUtils.java
Add untracked>> <press enter>
added 1 path

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> q

If you now write git status, you can see, that your chosen file (FileUtils.java) is added.

I would like to git add one of the above files, is there a quick way to do so without having to type out the entire path?

You can use a pattern to add the specific file you want. To add the last file in your example, you can do:

$ git add *FileUtils*

This kind of thing is what the X terminal select/paste conventions are for: drag-select the name, middle-click paste behaves as if you'd typed the name without even touching the mouse, and it uses the "selection" buffer directly without going through the clipboard.

Related