当文件路径变得很长时,使用git add
命令会变得繁琐。例如:
git add src_test/com/abc/product/server/datasource/manager/aats/DSManger.java
是否有可能绕过指定绝对文件路径?也许可以使用某种模式或其他方法吗?
我知道我们可以使用git gui
,但我想用命令行完成。
提前感谢您的输入。
对于类Unix系统,您可以始终使用星号指向文件,例如:
git add *DSManager.java
将包括从您的当前工作目录开始在您的源代码树中Git可以找到的所有DSManager.java文件。
这里是另一种添加文件的方法。至少在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.
最后输入7
或q
退出。
/src/app/foo/bar/jemima/rhymer/filename.ts
。即使使用制表符补全也很烦人。 - Rin and Len使用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
cd src_test/com/abc/product/server/datasource/manager/aats
git add DSManger.java
#!/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