61

git add一旦文件路径变得冗长,使用命令就会变得乏味。例如 git add src_test/com/abc/product/server/datasource/manager/aats/DSManger.java
,是否可以绕过指定绝对文件路径?可能正在使用某种模式或其他东西?

我知道我们可以使用git gui. 但我想用 cmd 线来做。

提前感谢您的投入。

4

6 回答 6

65

对于类 unix 系统,您始终可以使用星号来指向文件,例如

 git add *DSManager.java

将包含 git 可以从当前工作目录开始的源代码树中找到的所有 DSManager.java 文件。

于 2011-06-06T07:36:38.670 回答
46

这是另一种添加文件的方法。至少在 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

2选择更新,或键入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

按与您要暂存的文件对应的数字。用逗号分隔多个数字,例如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>>

只需按[enter]这里。

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.

最后输入7orq退出。

于 2013-04-10T00:22:54.877 回答
35

使用 bash,您可以设置 "globstar" ( shopt -s globstar),然后执行以下操作:

git add **/DSManger.java

添加当前目录下存在的所有名为 DSManager.java 的文件。

**/匹配所有目录和子目录。)

于 2011-06-06T07:37:14.177 回答
3

我不确定我是否理解你的问题。

要添加所有文件(尚未添加),请使用:

git add .

如果您需要添加除一个文件之外的所有文件,请冷添加所有文件,然后使用以下命令删除文件:

git reset HEAD <file>

您还可以在子目录中添加所有文件

git add subdir/

我知道可能令人讨厌的一件事是,当您重命名文件时,您需要添加新文件名和 git rm 旧名称。重命名目录时,这可能很烦人。这个(仅限 unix)git 别名解决了这个问题(把它放在你的 ~/.gitconfig 文件中:

[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

这会添加所有新文件并删除所有已删除文件并将其暂存到索引中。

于 2011-06-06T07:40:29.530 回答
2

如果您的终端窗口当前 cd 进入正确的文件夹(src_test/com/abc/product/server/datasource/manager/aats),我相信您可以只说“git add DSManger.java”。所以就这样做:

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

否则,除非您进行单独的回购,否则我想不出任何其他方式。在此处输入图像描述

于 2011-06-06T07:32:35.093 回答
0

请查看我为此目的创建的这个示例 bash 脚本。链接到 Github 存储库

#!/bin/bash
# Script Name: git-bash.sh
#
# Author: Krishnadas P.C<pckrishnadas88@gmail.com>
# 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.
'

样本输出

$ ./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
于 2018-05-06T01:25:49.293 回答