git 命令的使用(一)


这里只是git命令的介绍,其他概念不讨论。原文地址gitref.org   本人英语水平有限,只是根据自己理解而翻译(内容有删改)。请使用目录查看:


创建和获取仓库

在Git做任何事之前,你必须有一个Git仓库

一般有两种方式来得到一个Git仓库。一种方法是创建一个新的仓库,第二个方法是克隆一个。

init

创建一个仓库在现有的目录文件,您可以简单地在该目录下运行git init。例如,假设我们有一个目录和几个文件,如下:

$ cd konichiwa
$ ls
README hello.rb

这是一个项目,我们正在写作的例子“Hello World”程序。到目前为止,我们只是有一个Ruby文件,这仅仅是一个开始。要Git版本控制这个项目,我们可以简单地运行git init。

$ git init
Initialized empty Git repository in /opt/konichiwa/.git/

现在你可以看到有一个.git的子目录。这就是你的git仓库。
$ ls -a
. .. .git README hello.rb

恭喜,你现在有一个Git仓库库骨架,可以开始你的项目的版本控制。
简而言之,您可以使用git init使现有目录的内容创建一个新的git仓库。你可以在任何时间在任何目录创建。

clone

克隆一个仓库
如果你需要与某人协作项目,或者如果你找到一份项目,你就可以看着或使用的代码,你只需克隆它。运行git clone [url]  。URL为你要克隆仓库的地址。

$ git clone git://github.com/schacon/simplegit.git
Initialized empty Git repository in /private/tmp/simplegit/.git/
remote: Counting objects: 100, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 100 (delta 35), reused 0 (delta 0)
Receiving objects: 100% (100/100), 9.51 KiB, done.
Resolving deltas: 100% (35/35), done.
$ cd simplegit/
$ ls
README Rakefile lib
这将复制项目的源码到本地仓库,这样你可以查看代码或编辑它。您可以看到  .git目录——这是整个项目的数据。默认情况下,git 将建立一个名为simpegit的目录,如果你想建立不同的目录名,只需 在 git clone [url] 加上目录名即可。

$ ls -a
. .. .git README Rakefile lib
$ cd .git
$ ls
HEAD description info packed-refs
branches hooks logs refs
config index objects

简而言之,您可以使用git 克隆得到的git仓库的本地拷贝,这样你就可以查看它或修改它。

基本的命令

Git是一个分布式版本控制系统。本节将解释如何在开发当中使用git。您将使用git add 开始跟踪新文件并更改已经跟踪文件,然后git status和git diff查看已修改的记录,最后git commit来记录你的修改到仓库。这将是基本的工作流程也是最经常使用的几条命令。

 git add

添加修改过的的内容到仓库记录。
在Git中,你必须添加修改过的的内容到记录才能提交。如果文件是新文件,则添加到仓库中。以前已经添加了的文件被修改过则将修的内容添加。
回到我们的Hello World示例,一旦我们启动这个项目,我们将开始把我们的文件添加到仓库里。我们可以使用git add使用git status,查看我们的项目的状态。

$ git status -s
?? README
?? hello.rb

所以,我们有两个未被跟踪的文件,现在把他添加:
$ git add README hello.rb

现在,如果再运行 git status -s,可以看到它们已经被添加:
$ git status -s
A README
A hello.rb

也可以一次将目录下所有未添加的文件递归的的添加,使用 git add  .即可。还可以用 git add *但是这样不是递归的,所以子目录里的文件将不会被添加。OK,现在如果我们编辑这些文件并运行 git status 将看到奇怪的事情。
$ vim README
$ git status -s
AM README
A hello.rb

‘AM’的意思是这个文件在上次添加后被修改过,这意味着如果我们现在提交代码,仓库快照里存的是上一次添加后的内容,而不是现在本地电脑里的内容。git 并不会多管闲事,自动的把修改过的内容添加,你需要命令git 做你想让它做的事。

git status

当你看到在git添加部分,为了查看当前缓存的状态, 您可以运行git status命令。使用- s选项将给你短输出。没有这个标志git status命令将给你更多的内容和提示。这是相同的状态输出和无s参数。

$ git status -s
AM README
A hello.rb

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README
# new file: hello.rb
#
# 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: README
#

你可以很清楚看明白段输出和长输出的区别,根据需要使用不同的命令。

git diff

开发人员在本地进行开发后,可以使用git diff查看改动。
在没有额外的参数情况下,一个简单的git diff将显示上次提交以来的修改的代码或内容,显示为diff格式(a patch)。

$ vim hello.rb
$ git status -s
M hello.rb
$ git diff
diff --git a/hello.rb b/hello.rb
index d62ac43..8d15d50 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1,7 +1,7 @@
class HelloWorld

def self.hello
- puts "hello world"
+ puts "hola mundo"
end

end

所以git status 显示的是文件的修改状态,git diff 显示的是 具体修改了什么。
$ git status -s
M hello.rb
$ git add hello.rb
$ git status -s
M hello.rb
$ git diff
$ 

如果你想查看已经载入的修改,使用 git diff --cached
$ git status -s
M hello.rb
$ git diff
$
$ git diff --cached
diff --git a/hello.rb b/hello.rb
index d62ac43..8d15d50 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1,7 +1,7 @@
class HelloWorld

def self.hello
- puts "hello world"
+ puts "hola mundo"
end

end

git diff HEAD
git diff HEAD  显示所有载入的和为被载入的修改
$ vim hello.rb
$ git diff
diff --git a/hello.rb b/hello.rb
index 4f40006..2ae9ba4 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1,7 +1,7 @@
class HelloWorld

+ # says hello
def self.hello
puts "hola mundo"
end

end
$ git diff --cached
diff --git a/hello.rb b/hello.rb
index 2aabb6e..4f40006 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1,7 +1,7 @@
class HelloWorld

def self.hello
- puts "hello world"
+ puts "hola mundo"
end

end
$ git diff HEAD
diff --git a/hello.rb b/hello.rb
index 2aabb6e..2ae9ba4 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1,7 +1,8 @@
class HelloWorld

+ # says hello
def self.hello
- puts "hello world"
+ puts "hola mundo"
end

end

git diff --stat

显示修改的概要 而不是全部。如果你想要查看修改的内容但不想看详细的修改,可以使用 --stat参数。

$ git status -s
MM hello.rb
$ git diff --stat
hello.rb | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ git diff --cached --stat
hello.rb | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
$ git diff HEAD --stat
hello.rb | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)

你可以在任何一条命令后 跟一个路径,这样就会指定你要查看的文件范围。


git commit

现在,你已经添加了了内容想将它们快照下来,可以运行git commit 提交修改,git需要记录下你的名字和email。你可以这样添加:

$ git config --global user.name 'Your Name'
$ git config --global user.email you@somedomain.com

让我们把修改提交吧,-m 后面加上你的提交信息。
$ git add hello.rb
$ git status -s
M hello.rb
$ git commit -m 'my hola mundo changes'
[master 68aa034] my hola mundo changes
1 files changed, 2 insertions(+), 1 deletions(-)

现在我们已经记录了快照。如果我们运行git status ,我们将看到一个“干净的工作目录”,这意味着自从我们上次提交我们还没有作出任何改变。
$ git status
# On branch master
nothing to commit (working directory clean)

如果 你没有添加-m 的参数,那么提交是git将打开文本编辑器提示你:需要输入提交信息:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: hello.rb
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

正常情况下,一个好的提交信息是很重要的。在一个开源项目中,经常遇到这样的信息
Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body. The blank
line separating the summary from the body is critical (unless you omit
the body entirely); some git tools can get confused if you run the
two together.

Further paragraphs come after blank lines.

- Bullet points are okay, too

- Typically a hyphen or asterisk is used for the bullet, preceded by a
single space, with blank lines in between, but conventions vary
here

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: hello.rb
#
~
~
~
".git/COMMIT_EDITMSG" 25L, 884C written


提交信息是非常重要的。因为大部分的Git项目在精心制作提交本地,然后分享后,它非常强大,能够写三个或四个提交的逻辑上独立的变更,以便你的工作可能更容易收到同行评议。一个单独的提交与一个漂亮的提交消息所以他们更容易看到你在做什么和为什么。

git commit -a

如果你认为git添加阶段的工作流程太繁琐,Git允许您跳过这部分 使用- a选项. 这告诉git 任何文件在修改之后不需再git add 就可以提交。

$ vim hello.rb
$ git status -s
M hello.rb
$ git commit -m 'changes to hello file'
# On branch master
# 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: hello.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -am 'changes to hello file'
[master 78b2670] changes to hello file
1 files changed, 2 insertions(+), 1 deletions(-)

git reset

撤销修改和提交
git reset
可能是最令人迷惑的一条命令了, 但在你需要的时候又是最有用的。 有三种具体的使用方法:
git reset HEAD 撤销上次提交:

$ git status -s
M README
M hello.rb
$ git add .
$ git status -s
M README
M hello.rb
$ git reset HEAD -- hello.rb
Unstaged changes after reset:
M hello.rb
$ git status -s
M README
M hello.rb

git reset --hard HEAD     撤销最后一次提交并清除本地修改。
git reset SHA1                  回到SHA1对应的提交状态。

git rm

把文件从仓库删除

git rm 会移除仓库中的记录。这和 git reset HEAD 有些不同 。 他的非正式用法还包括删除文件,但是它只是在提交之前删除文件。

git stash

可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个commit, 改完bug之后再stash pop, 继续原来的工作。
基础命令:

$git stash
$do some work
$git stash pop

进阶:
当你多次使用’ git stash’命令后,你的栈里将充满了未提交的代码,这时候你会对将哪个版本应用回来有些困惑,’ git stash list’命令可以将当前的Git栈信息打印出来,你只需要将找到对应的版本号,例如使用’git stash apply stash@{1}’就可以将你指定版本号为stash@{1}的工作取出来,当你将所有的栈都应用回来的时候,可以使用’git stash clear’来将栈清空
$ git status -s
M hello.rb
$ git stash
Saved working directory and index state WIP on master: 5857ac1 hello with a flower
HEAD is now at 5857ac1 hello with a flower
$ git status
# On branch master
nothing to commit (working directory clean)
$ vim hello.rb
$ git commit -am 'it stops raining'
[master ee2d2c6] it stops raining
1 files changed, 1 insertions(+), 1 deletions(-)
$ vim hello.rb
$ git stash
Saved working directory and index state WIP on master: ee2d2c6 it stops raining
HEAD is now at ee2d2c6 it stops raining
$ git stash list
stash@{0}: WIP on master: ee2d2c6 it stops raining
stash@{1}: WIP on master: 5857ac1 hello with a flower





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值