2

For example if I have 8 different files that need to be committed but 4 of them I want to commit together, how can I add just those 4 without having to type out the full path or copy/paste?

Edit: More specifically I am looking for a way to choose from a numbered list:

Fake Example:

Git status

File.txt
File.txt
File.txt
File.txt
File.txt
File.txt

Git add 1,3,6
  • You'll have to specify them *somehow*: you can cd to their common parent directory and use relative paths, but that's about it. – NickD Jun 10 '18 at 04:15
  • Hmm that’s what I thought. I was hoping there was a way to specify the line item you want to add – Chase Douglas Jun 10 '18 at 04:18
  • Of course, if there is a pattern, you might be able to use shell mechanisms to take advantage (globbing as one answer suggests, a common directory, a find command, etc.) But in general, full or relative pathnames are going to be necessary. – NickD Jun 10 '18 at 04:25
  • Possible duplicate of [How to add or commit specific files without specifying their full path?](https://stackoverflow.com/questions/44015342/how-to-add-or-commit-specific-files-without-specifying-their-full-path) – phd Jun 10 '18 at 08:59

1 Answers1

4

It depends on the names of the files. If there's a pattern that can be used to distinctly identify the files to be added, you may be able to use a glob to specify the subset with git add.

An example:

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file1.txt
        file2.txt
        file3.txt
        file4.txt
        file5.txt
        file6.txt
        file7.txt
        file8.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git add file[1-4].txt
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   file1.txt
        new file:   file2.txt
        new file:   file3.txt
        new file:   file4.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file5.txt
        file6.txt
        file7.txt
        file8.txt

Alternatively, you can use git add --interactive:

$ git add --interactive

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 4
  1: file1.txt
  2: file2.txt
  3: file3.txt
  4: file4.txt
  5: file5.txt
  6: file6.txt
  7: file7.txt
  8: file8.txt
Add untracked>> 1-4
* 1: file1.txt
* 2: file2.txt
* 3: file3.txt
* 4: file4.txt
  5: file5.txt
  6: file6.txt
  7: file7.txt
  8: file8.txt
Add untracked>>
added 4 paths

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 7
Bye.
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   file1.txt
        new file:   file2.txt
        new file:   file3.txt
        new file:   file4.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file5.txt
        file6.txt
        file7.txt
        file8.txt
chuckx
  • 6,484
  • 1
  • 22
  • 23
  • Interesting. What if the 4 I want to add are all css files from the same site? Is there some way to add only files from that site all at once? – Chase Douglas Jun 10 '18 at 04:22
  • I don't know what you mean by "site". It really all boils down to the file/directory naming. If you provide a concrete example of the naming you're dealing with, then more specific guidance is possible. – chuckx Jun 10 '18 at 04:23
  • Your last edit answered my question. Sorry I’m just learning all the terms for using git in terminal. I have been using sourcetree since day 1 – Chase Douglas Jun 10 '18 at 04:25
  • for adding css files alone use "git add *.css" (without quotes). Beware this will add all css files in the current directory to staging area. – SRIDHARAN Jun 10 '18 at 06:31
  • That assumes all the *.css file are in the current working directory. Once again, to provide specific guidance, details regarding the files and directories involved is necessary. – chuckx Jun 10 '18 at 06:32