[英]How to avoid specifying absolute file path while git-add
一旦文件路徑變長,使用git add
命令變得乏味。 例如git add src_test/com/abc/product/server/datasource/manager/aats/DSManger.java
是否可以繞過指定絕對文件路徑? 可能正在使用某種模式或其他東西?
我知道我們可以使用git gui
。 但我想用 cmd 線來做。
提前感謝您的投入。
對於類 unix 系統,您始終可以使用星號來指向文件,例如
git add *DSManager.java
將包括所有 DSManager.java 文件 git 可以在您的源代碼樹中找到,從您當前的工作目錄開始。
這是另一種添加文件的方法。 至少在 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
到 select 更新,或輸入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.
最后輸入7
或q
退出。
使用 bash,您可以設置“globstar”( shopt -s globstar
),然后執行以下操作:
git add **/DSManger.java
添加當前目錄下存在的所有名為 DSManager.java 的文件。
( **/
匹配所有目錄和子目錄。)
我不確定我是否理解你的問題。
要添加所有文件(尚未添加),請使用:
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
這會添加所有新文件並刪除所有已刪除文件並將其暫存到索引中。
如果您的終端 window 當前 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
否則,除非您進行單獨的回購,否則我想不出任何其他方式。
請查看我為此目的創建的示例 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.
'
樣品 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.