如何同时向 Git 添加多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19576116/
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
How to add multiple files to Git at the same time
提问by kishore .
This will be my first git use. I have added new files ( a lot ) to the folder/project ( git local repository).
这将是我第一次使用 git。我已将新文件(很多)添加到文件夹/项目(git 本地存储库)。
I went through online tutorials and forums and see i can do
我浏览了在线教程和论坛,看看我能做到
git commit -a
So i go to the base folder of the repository and do a
所以我转到存储库的基本文件夹并执行
sudo git commit -a
But then, some screens comes up and asks me to add a comment which i do. i do not know how to proceed or exit. I do not want to mess up so i did ctrl + Z and did not do anything.
但是,一些屏幕出现并要求我添加我所做的评论。我不知道如何继续或退出。我不想搞砸,所以我做了 ctrl + Z 并没有做任何事情。
Can you guys please outline the commands i need to use?
你们能概括一下我需要使用的命令吗?
git commit -a
and
和
git push?
回答by Reggie Pinkham
Use the git add
command, followed by a list of space-separated filenames.
使用该git add
命令,后跟以空格分隔的文件名列表。
git add file-name-1 file-name-2 file-name-3
回答by dax
To add all the changes you've made:
要添加您所做的所有更改:
git add .
git add .
To commit them:
提交它们:
git commit -m "MY MESSAGE HERE"
#-m is the message flag
git commit -m "MY MESSAGE HERE"
#-m 是消息标志
You can put those steps together like this:
您可以将这些步骤放在一起,如下所示:
git commit -a -m "MY MESSAGE HERE"
git commit -a -m "MY MESSAGE HERE"
To push your committed changes from your local repository to your remote repository:
要将已提交的更改从本地存储库推送到远程存储库:
git push origin master
git push origin master
You might have to type in your username/password for github after this. Here'sa good primer on using git. A bit old, but it covers what's going on really well.
在此之后,您可能必须输入 github 的用户名/密码。 这是使用 git 的很好的入门书。有点旧,但它涵盖了正在发生的事情。
回答by nadalsol
You can also select multiple files like this
您也可以像这样选择多个文件
git add folder/subfolder/*
This will add all the files in the specified subfolder. Very useful when you edit a bunch of files but you just want to commit some of them...
这将添加指定子文件夹中的所有文件。当您编辑一堆文件但只想提交其中一些文件时非常有用...
回答by Fabio Antunes
As some have mentioned a possible way is using git interactive staging. This is great when you have files with different extensions
正如一些人所提到的,一种可能的方法是使用git interactive staging。当您有不同扩展名的文件时,这很棒
$ git add -i
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
If you press 2
then enter
you will get a list of available files to be added:
如果按2
,然后enter
你会得到可用的文件列表中添加:
What now> 2
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>
Now you just have to insert the number of the files you want to add, so if we wanted to add TODO
and index.html
we would type 1,2
现在你只需要插入你想要添加的文件数量,所以如果我们想添加TODO
,index.html
我们会输入1,2
Update>> 1,2
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>
You see the *
before the number? that means that the file was added.
你看到*
前面的数字了吗?这意味着该文件已添加。
Now imagine that you have 7 files and you want to add them all except the 7th? Sure we could type 1,2,3,4,5,6
but imagine instead of 7 we have 16, that would be quite cumbersome, the good thing we don't need to type them all because we can use ranges,by typing 1-6
现在想象一下,您有 7 个文件,并且想要添加除第 7 个之外的所有文件?当然我们可以输入1,2,3,4,5,6
但是想象一下我们有 16 个而不是 7 个,这会很麻烦,我们不需要全部输入它们,因为我们可以使用范围,通过输入1-6
Update>> 1-6
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
* 4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
7: unchanged +5/-1 file7.html
Update>>
We can even use multiple ranges, so if we want from 1 to 3 and from 5 to 7 we type 1-3, 5-7
:
我们甚至可以使用多个范围,所以如果我们想要从 1 到 3 和从 5 到 7,我们输入1-3, 5-7
:
Update>> 1-3, 5-7
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
* 7: unchanged +5/-1 file7.html
Update>>
We can also use this to unstage files, if we type -number
, so if we wanted to unstage file number 1 we would type -1
:
如果我们输入-number
,我们也可以使用它来取消暂存文件,因此如果我们想取消暂存文件编号 1,我们将键入-1
:
Update>> -1
staged unstaged path
1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
* 7: unchanged +5/-1 file7.html
Update>>
And as you can imagine we can also unstage a range of files, so if we type -range
all the files on that range would be unstaged. If we wanted to unstage all the files from 5 to 7 we would type -5-7
:
您可以想象我们还-range
可以取消暂存范围的文件,因此如果我们键入该范围内的所有文件,则将取消暂存。如果我们想取消从 5 到 7 的所有文件,我们将输入-5-7
:
Update>> -5-7
staged unstaged path
1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
5: unchanged +5/-1 file5.html
6: unchanged +5/-1 file6.html
7: unchanged +5/-1 file7.html
Update>>
回答by EliuX
If you want to add multiple files in a given folder you can split them using {,}
. This is awesome for not repeating long paths, e.g.
如果要在给定文件夹中添加多个文件,可以使用{,}
. 这对于不重复长路径来说很棒,例如
git add long/path/{file1,file2,...,filen}
Beware not to put spaces between the ,
.
注意不要在,
.
回答by somi
When you change files or add a new ones in repository you first must stage them.
当您在存储库中更改文件或添加新文件时,您首先必须暂存它们。
git add <file>
or if you want to stage all
或者如果你想全部上演
git add .
By doing this you are telling to git what files you want in your next commit. Then you do:
通过这样做,你是在告诉 git 你在下一次提交中想要什么文件。然后你做:
git commit -m 'your message here'
You use
你用
git push origin master
where origin is the remote repository branch and master is your local repository branch.
其中 origin 是远程存储库分支, master 是您的本地存储库分支。
回答by Greg Hewgill
It sounds like git is launching your editor (probably vi
) so that you can type a commit message. If you are not familiar with vi
, it is easy to learn the basics. Alternatives are:
听起来 git 正在启动您的编辑器(可能vi
),以便您可以输入提交消息。如果你不熟悉vi
,很容易学习基础知识。替代方案是:
Use
git commit -a -m "my first commit message"
to specify the commit message on the command line (using this will not launch an editor)Set the
EDITOR
environment variable to an editor that you are familiar with
用于
git commit -a -m "my first commit message"
在命令行上指定提交消息(使用它不会启动编辑器)将
EDITOR
环境变量设置为您熟悉的编辑器
回答by Johnson Ogwuru
If you want to stage and commit all your files on Github do the following;
如果您想在 Github 上暂存和提交所有文件,请执行以下操作;
git add -A
git commit -m "commit message"
git push origin master