git 如何避免在git-add时指定绝对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6248917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:33:13  来源:igfitidea点击:

How to avoid specifying absolute file path while git-add

gitgit-add

提问by Vaman Kulkarni

Using git addcommand 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?

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

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

我知道我们可以使用git gui. 但我想使用 cmd 行来做到这一点。

Thanks in advance for the inputs.

提前感谢您的投入。

采纳答案by Steffen

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

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

 git add *DSManager.java

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

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

回答by enzipher

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

这是添加文件的另一种方法。至少在 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 2to select update, or type u.

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

Press the number corresponding to the file you want to stage. Separate multiple numbers with a comma, e.g. 1,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>>

Just press [enter]here.

只需按[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.

Finally type 7or qto quit.

最后输入7q退出。

回答by dogbane

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

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

git add **/DSManger.java

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

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

(**/matches all directories and subdirectories.)

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

回答by Stein G. Strindhaug

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:

我知道很烦人的一件事是,当您重命名文件时,您需要添加新文件名并使用 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

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

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

回答by Jon

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”。所以只需这样做:

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.

否则,除非您制作单独的回购协议,否则我想不出任何其他方式。

回答by Krishnadas PC

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

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

#!/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 [  == "st" ];
   then
       git  ${gitstaged[]}
   elif [  == "nt" ]; 
   then  
    git  ${gitnotstaged[]}
   elif [  == "ut" ]; 
   then  
    git  ${gituntracked[]}
   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