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

发布于 2024-11-14 03:48:23 字数 252 浏览 2 评论 0原文

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

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

预先感谢您的投入。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猥琐帝 2024-11-21 03:48:23

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

 git add *DSManager.java

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

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.

北凤男飞 2024-11-21 03:48:23

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

最后输入7q 退出。

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.

杀お生予夺 2024-11-21 03:48:23

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

git add **/DSManger.java

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

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

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.)

比忠 2024-11-21 03:48:23

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

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

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

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

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.

苄①跕圉湢 2024-11-21 03:48:23

我相信如果您的终端窗口当前已 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

否则,除非你制作一个单独的存储库,否则我想不出任何其他方法。在此处输入图像描述

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

南薇 2024-11-21 03:48:23

请查看我为此目的创建的示例 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 [ $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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文