5

git status gives a list of modified files :

modified : app/controllers/AppController.controller.php
modified : app/controllers/Front.controller.php
modified : app/models/Booking.model.php
modified : app/models/Price.model.php
modified : app/views/AdminBookings/update.php
modified : app/views/Layouts/elements/leftmenu.php
...

I find it inconvenient to "type" (whatever the way) the file path every time I have to add, checkout or log a file.

Is there any way to associate an ID to these files so I can individually manage them faster ? Like so :

[0] modified : app/controllers/AppController.controller.php
[1] modified : app/controllers/Front.controller.php
[2] modified : app/models/Booking.model.php
[3] modified : app/models/Price.model.php
[4] modified : app/views/AdminBookings/update.php
[5] modified : app/views/Layouts/elements/leftmenu.php
...

$ git add --id 3
kursus
  • 1,396
  • 3
  • 19
  • 35

3 Answers3

4

Try interactive adding using git add -i. It will put you in a screen like this:

           staged     unstaged path
  1:    unchanged       +45/-5 app/controllers/AppController.controller.php
  2:    unchanged        +4/-2 app/controllers/Front.controller.php
  3:    unchanged       +15/-5 app/models/Booking.model.php

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

From there, you can choose update (or u in short) to go into the adding mode

What now> u
           staged     unstaged path
  1:    unchanged       +45/-5 app/controllers/AppController.controller.php
  2:    unchanged        +4/-2 app/controllers/Front.controller.php
  3:    unchanged       +15/-5 app/models/Booking.model.php
Update>>

From the update prompt, you can now select the files you want to add to the index. Simply enter the number of the file you want to add. If you have a colorful output, you can also see that some parts of the file path are in blue which highlights the text you can enter to refer to that file.

You can stage multiple files, e.g. to stage the first and the third, enter 1 and press return, then enter 3 and press return. Notice the stars in front of the files that indicate that you staged them. Once you are done, press enter on the empty update prompt to leave. You will return in the start screen which you can exit by entering q.

poke
  • 369,085
  • 72
  • 557
  • 602
3

I do not quite like the interactive mode - I find it to be too slow for what I was looking for. I wrote a custom git command, called git-a. This command must be on your PATH, and must be an executable. It is based on the output of the command git status -s, in order to avoid further string filtering, greping and the likes.


exclusionList=('M', 'MM', 'MD', 'MMD', 'A', 'AM', 'AD', 'AMD', 'D', 'DR', 'DC',\
'RM','RD', 'RMD', 'C', 'CM', 'CD', 'CMD', 'DD', 'AU', 'UD', 'UA', 'DU', 'AA',\
'UU', '??', '!!', 'R')

allEntries=( $(git status -s) )
relevantEntries=()
previousEntry=""
for currentEntry in "${allEntries[@]}"; do
    if ! echo ${exclusionList[@]} | grep -q -w -- "$currentEntry"
    then
            # if the currenty entry is the '->', that means this is a file
            # rename - therefore, the old name (first after the letter) should
            # be removed from the array, and the new name used instead
            if [ "$currentEntry" == '->' ]
            then
                unset 'relevantEntries[${#relevantEntries[@]}-1]'
            else
                relevantEntries+=("$currentEntry")
            fi
            previousEntry="$currentEntry"
    fi
done
git add ${relevantEntries[$(( $1 - 1 ))]}

It is quite manual, I know, but, you can track all possible status outputs from git docs. Essentially, it is a list comprehension, but in bash.

Note that this can be used for any command you like - just change the last line, and it will work just the same. I'm currently thinking of a way to get the relevantEntries only, making it available for any git command, but I'm not sure how to do it just yet.

I'm currently creating the file with a name like, say, git-custom-add, and aliasing it in my .gitconfig file as cadd, so I can use all parameters I see suit, and still keep the command short.

Lucas Lima
  • 832
  • 11
  • 23
  • Maybe use`git diff --name-only HEAD` for `relevantEntries`? You can then filter out easily along type, for example with `--diff-filter=uxm` to exclude unmerged/unknown/modified paths. – Romain Valeri Jan 03 '20 at 15:28
  • 1
    @RomainValeri I'm using `git status` here because `git diff` will only output files currently in the index, whereas the former will list everything, untracked included. Sure, you could work that around by using the `--intent-to-add` option for `git add`, but then it defeats the whole purpose of simplifying the workflow, imo – Lucas Lima Jan 03 '20 at 18:26
0

You can add by path

git add models/

or all files:

git add .

or by mask

Recursively add files by pattern

Community
  • 1
  • 1