[英]Add items to git index efficiently
I find it very tedious to write full paths when adding changed files to index. 我发现在将更改的文件添加到索引时编写完整路径非常繁琐。 For example, i've changed 3 files, but want to commit only 2 of them:
例如,我已经更改了3个文件,但只想提交其中的2个:
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: apps/frontend/config/modules/file1
# modified: apps/frontend/config/modules/file2
# modified: apps/frontend/config/modules/file3
So I have to type as much as: 所以我必须打字:
git add app/frontend/modules/file1 app/frontend/modules/file3
I am looking for a way to add items by their index in the git status list? 我正在寻找一种方法来在git状态列表中按索引添加项目? Something like
就像是
git add %1 %3
Use interactive
add: 使用
interactive
添加:
git add -i
It'll ask you for each file, if you want to add it to the commit. 如果要将其添加到提交中,它会询问您每个文件。
You can even go ahead and pick based on patches, which is always useful: 您甚至可以根据补丁进行选择,这总是有用的:
git add -p
cd apps/frontend/config/modules
git add file1 file3
cd - #go back
您还可以使用shell扩展:
git add app/frontend/modules/file{1,3}
I've been using a similar script I wrote to add only 1 file, eg git nadd N
to add the N'th modified file. 我一直在使用我写的类似脚本只添加1个文件,例如
git nadd N
来添加第N个修改过的文件。 It shouldn't be hard to modify it to allow several files. 修改它以允许多个文件应该不难。
#!/bin/bash
if [[ $# != 1 ]] ; then
echo "usage: git nadd <index>"
exit 0
fi
num=$1
# get unstaged modified files
modified_files=$(git status --porcelain | grep " M " | cut -c 4-)
if [[ $modified_files == "" ]] ; then
echo "error: there are no modified files"
exit 1
fi
# count how many we have
num_modified_files=$(echo "$modified_files" | wc -l)
if [[ $num -gt $num_modified_files ]] ; then
echo "error: index larger than number of modified files"
exit 2
fi
# pick n'th modified file
file=$(echo "$modified_files" | sed -n "$num p")
# fix $file to full path in case we're not in root
root=$(git rev-parse --show-cdup)
file=$root$file
git add -- "$file"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.