简单的 GIT 命令序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10658195/
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
Simple Sequence of GIT Commands
提问by RGi
I read the documentation and googled a good bit, but there is no real simple steps to be able to commit your local changes to github. I compiled the following steps and I just want to make sure am doing the right thing. If I changed a file foo.java locally:
我阅读了文档并在谷歌上搜索了很多,但没有真正简单的步骤能够将您的本地更改提交到 github。我编译了以下步骤,我只是想确保我做的是正确的事情。如果我在本地更改了文件 foo.java:
git status -s //will show me that foo.java has changed
git add foo.java //will add it to my local repo
git commit -m "my changes" //commit to the local repo
git tag "v1.1" //create a tag
git push --tags //finally, move the local commit to the remote repo with the new tag. this will prompt for your password. if no tag is set as in step 4, then just
git push
git status -s // 将显示 foo.java 已更改
git add foo.java //将它添加到我的本地仓库
git commit -m "my changes" //提交到本地仓库
git tag "v1.1" //创建一个标签
git push --tags //最后,将本地提交移动到带有新标签的远程仓库。这将提示您输入密码。如果没有在步骤 4 中设置标签,则只需
推
is enough. right?
足够。对?
I am just trying to make sure that these basic steps for the most use cases is what is required to use github. I am brand new to github, and these steps are working for me, but want to make sure i am not making any fundamental mistake. Please comment if there are any missing steps. Again, am concerned about the most generic day-to-day use (like, am not really concerned about branches, etc. which I will learn on a need basis). Thank you in advance.
我只是想确保大多数用例的这些基本步骤是使用 github 所需的。我是 github 的新手,这些步骤对我有用,但我想确保我没有犯任何根本性的错误。如果有任何遗漏的步骤,请发表评论。同样,我担心最通用的日常使用(例如,我并不真正关心分支等,我将根据需要学习这些)。先感谢您。
回答by simont
Your steps are fine. To nit-pick slightly, though, about the comments:
你的步骤没问题。不过,要稍微挑一下评论:
The comments about step (2) and (3) are not the best way to think about what's happening, I don't believe.
关于步骤 (2) 和 (3) 的评论并不是思考正在发生的事情的最佳方式,我不相信。
2.git add foo.java //will add it to my local repo
3.git commit -m "my changes" //commit to the local repo
The step which "adds" your file to the local repositoryis git-commit
. That's why it's called commit
; you commitchanges to the repository. git-add foo
adds foo
to the staging area, notto the repo itself.
将您的文件“添加”到本地存储库的步骤是git-commit
. 这就是为什么它被称为commit
; 您提交对存储库的更改。git-add foo
添加foo
到staging area,而不是repo 本身。
Your git
repository has three "areas", working
, staging
and repository
, depicted here (image taken from the Pro Git book):
您的git
存储库具有三个“区域”、working
、staging
和repository
,如下所示(图片取自Pro Git 书籍):
You make changes and work in the creatively named "working directory".
您可以在创造性命名的“工作目录”中进行更改和工作。
When you've made some changes, you want to prepare to make a commit. This is where the "staging area" comes into play. You "stage" the changes that you want to commit, and when you're happy with what the commit will look like, you commit the "staging area" to the "repository". [Note: in the man
pages, this staging area
is mostly referred to the index
].
当您进行了一些更改时,您想要准备进行提交。这就是“集结区”发挥作用的地方。您“暂存”要提交的更改,当您对提交的样子感到满意时,将“暂存区”提交到“存储库”。[注意:在man
页面中,这staging area
主要是指index
]。
This allows you a lot of flexibility. You can stage all the changes since your last commit, or you can stage files individually, or you can stage parts of files. You can add and delete files from the staging area without losing changes or messing up the repositories history. That's what the git add
and git rm
commands do; they add from the working directory
to the staging area
, but they don'tadd directly into the repository
. (Hopefully the image helps make the distinctions clear).
这为您提供了很大的灵活性。您可以暂存自上次提交以来的所有更改,也可以单独暂存文件,或者可以暂存部分文件。您可以从暂存区添加和删除文件,而不会丢失更改或弄乱存储库历史记录。这就是git add
andgit rm
命令的作用;它们从 the 添加working directory
到staging area
,但它们不会直接添加到repository
. (希望图像有助于区分清楚)。
Your steps are fine. If you want to understand more about branching, commiting, manipulating commits and branches and whatnot, I'd recommend reading the Pro Git book- it's got a whole bunch of pretty pictures and language simple enough that I can understand it ;)
你的步骤没问题。如果你想更多地了解分支、提交、操作提交和分支等等,我建议你阅读Pro Git 的书——它有一大堆漂亮的图片和简单的语言,我可以理解它;)
回答by sturmer
I think that that's enough for very basic usage. I'd just like to add two comments:
我认为这对于非常基本的使用来说已经足够了。我只想补充两条评论:
- It's always a good thing to check what you're adding to the staging area (which is what you're doing with
git add
): either usegit diff
, or do agit add --patch
, which will start an interactive procedure to let you decide whether to accept or reject each hunk of code you modified. If you messed anything up during this phase, you can alwaysgit reset HEAD
to get the changes back to the working copy (i.e., you would simply undo the add) - You might want to do steps 2 and 3 together by issuing a
git commit -a -m 'your message'
.
- 检查您要添加到暂存区的内容(这就是您正在使用的内容
git add
)总是一件好事:使用git diff
或执行 agit add --patch
,这将启动一个交互式过程,让您决定是接受还是拒绝每个您修改的大量代码。如果您在此阶段搞砸了任何事情,您可以随时git reset HEAD
将更改返回到工作副本(即,您只需撤消添加) - 您可能希望通过发出
git commit -a -m 'your message'
.
回答by ChrisB
After (3), you should be able to call git push origin master
which will push your current master
branch to github
在 (3) 之后,您应该可以调用git push origin master
which 将您当前的master
分支推送到 github