How to avoid specifying absolute file path while git-add
Asked Answered
E

6

66

Using git add command becomes tedious once the file path becomes lengthy. For e.g. git add src_test/com/abc/product/server/datasource/manager/aats/DSManger.java
Is it possible to bypass specifying absolute file path? May be using some kind of pattern or something?

I know that we can use git gui. But I want to do it using cmd line.

Thanks in advance for the inputs.

Escalator answered 6/6, 2011 at 7:26 Comment(0)
S
69

For unix-like systems you can always use the star to point to files, e.g.

 git add *DSManager.java

will include all DSManager.java files git can find within your source tree starting in your current working directory.

Stately answered 6/6, 2011 at 7:36 Comment(5)
Apparently, this ONLY adds newly created files, but skipps modified files. Any reason for that?Escalator
For me this also works with modified files. Maybe this depends on the git version, bash version, operating system. I don't know. I'm on OSX 10.6 with git 1.7.5.4 and bash 4.2.10(2).Stately
I'm afraid this doesn't add modified files, only new files, on Linux version 2.6.32 gcc version 4.4.7 Red Hat 4.4.7-17 (Godaddy Linux Hosting); git version 1.7.1Warton
This works on Windows through git bash.Neurasthenia
Works on Windows with powershell also.Neurasthenia
O
58

Here is another way to add files. Supported at the very least in git 1.7.1.

$ git add -i
           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
  2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt

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

Press 2 to select update, or type u.

           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
  2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt
Update>> 2

Press the number corresponding to the file you want to stage. Separate multiple numbers with a comma, e.g. 1,2.

           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
* 2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt
Update>>

Just press [enter] here.

updated one path

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

Finally type 7 or q to quit.

Orphism answered 10/4, 2013 at 0:22 Comment(8)
This is the correct answer. It is not shell dependent like globbing. Also, it can become difficult to write a good glob pattern to match only what you want to add when the file names can be very similar.Allianora
This has helped me a lot; my git version is 1.7.1 on a shared hosting account.Warton
nice mention, this helpsMadagascar
Also think this should be the accepted answer, very useful to me.Ability
this post just made my day :)Confucius
None of the options are for "restore", any reason why that is? I want to restore 3 files without having to /src/app/foo/bar/jemima/rhymer/filename.ts for each one. Even with tab-completion it's tedious.Outnumber
This has been what I was looking for to add only certain files when there are long paths to each. Thank you!Waiver
this is by far the best answer and should definitely be the accepted onePersuade
B
36

With bash, you can set "globstar" (shopt -s globstar) and then do:

git add **/DSManger.java

to add all files called DSManager.java present below the current directory.

(**/ matches all directories and subdirectories.)

Bixby answered 6/6, 2011 at 7:37 Comment(1)
That's actually the nicer solution, since it only includes files called DSManager.java and omits files called OtherDSManager.java. However, your shell has to support the globstar option.Stately
B
3

I'm not sure if I understand your question.

To add all files (not yet added), use:

git add .

If you need to add all but one file, you cold add all, then remove the files using:

git reset HEAD <file>

You can also add all files in a subdirectory with

git add subdir/

One thing that I know can be annoying is when you rename files, you need to add the new filename and git rm the old name. When renaming a directory this can be annoying. This (unix only) git alias solves this problem (put it in your ~/.gitconfig file:

[alias] ;add after this heading or create this heading if it does not exist
        addremove = !git add . && git ls-files --deleted | xargs --no-run-if-empty git rm

This adds all new files and removes all deleted files and stages it to the index.

Binal answered 6/6, 2011 at 7:40 Comment(1)
wasn't hard. Rename dir then git add -ALiederman
S
2

I believe you can just say "git add DSManger.java" if your terminal window is currently cd into the proper folder (src_test/com/abc/product/server/datasource/manager/aats). So just do:

cd src_test/com/abc/product/server/datasource/manager/aats
git add DSManger.java

Otherwise, I can't think of any other way unless you make a separate repo.enter image description here

Saucedo answered 6/6, 2011 at 7:32 Comment(1)
Right. That should work. But files to commit are from different packages hence it will be again tedious to everytime cdEscalator
C
1

Please have a look at this sample bash script which I have created for this purpose. Link to the Github Repo

#!/bin/bash
# Script Name: git-bash.sh
#
# Author: Krishnadas P.C<[email protected]>
# Date : 05-05-2018
#
# Description: A simple script to manipulate git files.
# TODO add more options and add Error Handlers. 

#declare color variables
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

#print the current git branch
echo "On Branch - $(git branch)"
#Get only staged files
gitstaged=($(git diff --name-only --cached))

#Get changes not staged for commit
gitnotstaged=($(git diff --name-only))

#Get only untracked files
gituntracked=($(git ls-files --others --exclude-standard))

if [ $# -ge 3 ];
then
   if [ $2 == "st" ];
   then
       git $1 ${gitstaged[$3]}
   elif [ $2 == "nt" ]; 
   then  
    git $1 ${gitnotstaged[$3]}
   elif [ $2 == "ut" ]; 
   then  
    git $1 ${gituntracked[$3]}
   else
     echo "Invalid input provied."
   fi     
fi
#Get the new status after the command has been executed.
gitstaged=($(git diff --name-only --cached))

#Get changes not staged for commit
gitnotstaged=($(git diff --name-only))

#Get only untracked files
gituntracked=($(git ls-files --others --exclude-standard))
#print the staged files.
for i in ${!gitstaged[@]}; do
   if [ $i -eq 0 ]; then 
    echo "Changes to be committed:" 
   fi
   echo "${green}st$i - ${gitstaged[$i]}${reset}"
done
#print the changes not staged files.
for i in ${!gitnotstaged[@]}; do
   if [ $i -eq 0 ]; then 
    echo "Changes not staged for commit:" 
   fi
   echo "${red}nt$i - ${gitnotstaged[$i]}${reset}"
done
#print the untracked files.
for i in ${!gituntracked[@]}; do
   if [ $i -eq 0 ]; then 
    echo "Untracked files:" 
   fi
  echo "${red}ut$i - ${gituntracked[$i]}${reset}"
done

: 'Example how to:
#$ ./git-bash.sh 
Untracked files
ut0 - git-bash.sh
ut1 - git-status.txt
ut2 - test
$./git-bash.sh add ut 0
Staged files
st0 - git-bash.sh
st1 - git-status.txt
Untracked files
ut0 - test
ut stands for untracked files.
nt stands for notstaged tracked files.
st stands for staged files.
'

Sample output

$ ./git-bash.sh 
On Branch - * master
Untracked files:
ut0 - git-bash.sh
ut1 - git-status.txt
ut2 - test

$ ./git-bash.sh add ut 2
On Branch - * master
Changes to be committed:
st0 - test
Untracked files:
ut0 - git-bash.sh
ut1 - git-status.txt
Chase answered 6/5, 2018 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.