GIT - 基本使用命令

一. 获取GIT仓库

有两种方法可以获取GIT工程项目:

1. 将一个已经存在的工程或目录导入到git

2. 克隆一个已经存在的git仓库

1. 初始化一个已经存在的工程(目录)

cd到工程的根目录,使用命令git init将其初始化

MacBook-Pro:~ $ cd ~/Project/my_repository/
MacBook-Pro:my_repository $ git init
Initialized empty Git repository in /Users/XXXXX/Project/my_repository/.git/
MacBook-Pro:my_repository $

2. 克隆一个已存在的仓库

MacBook-Pro:Project $ git clone https://github.com/libgit2/libgit2
Cloning into 'libgit2'...
remote: Counting objects: 76331, done.
remote: Compressing objects: 100% (21705/21705), done.
remote: Total 76331 (delta 53232), reused 76331 (delta 53232), pack-reused 0
Receiving objects: 100% (76331/76331), 35.72 MiB | 68.00 KiB/s, done.
Resolving deltas: 100% (53232/53232), done.
MacBook-Pro:Project $ ls
libgit2		my_repository	xlsx
MacBook-Pro:Project $

MacBook-Pro:Project $ git clone https://github.com/libgit2/libgit2 mylibgit
Cloning into 'mylibgit'...
remote: Counting objects: 76331, done.
remote: Compressing objects: 100% (21705/21705), done.
remote: Total 76331 (delta 53232), reused 76331 (delta 53232), pack-reused 0
Receiving objects: 100% (76331/76331), 35.72 MiB | 213.00 KiB/s, done.
Resolving deltas: 100% (53232/53232), done.
RossideMacBook-Pro:Project $ ls
libgit2		my_repository	xlsx	mylibgit
MacBook-Pro:Project $

二. 记录仓库变化

1. 检查仓库中文件的状态:在仓库目录下使用命令git status

MacBook-Pro:Project $ cd mylibgit/
MacBook-Pro:mylibgit $ ls
AUTHORS			COPYING			cmake			libgit2.pc.in
CHANGELOG.md		PROJECTS.md		deps			libgit2_clar.supp
CMakeLists.txt		README.md		docs			script
CODE_OF_CONDUCT.md	THREADING.md		examples		src
CONTRIBUTING.md		api.docurium		git.git-authors		tests
CONVENTIONS.md		appveyor.yml		include
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


nothing to commit, working tree clean
MacBook-Pro:mylibgit $

或者使用-s/--short输出状态的简写形式

MacBook-Pro:~ $ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt
MacBook-Pro:~ $ 

??表示的是一个新文件,还未被tracked。A 表示的是新文件,已经被加入stage了。M表示修改过的文件。可以看到简写形式中表示状态的标识是占据两个字符位的,左边的字符位标识的是staging area中的状态而右边的字符位则标识的是工作目录中的状态。

1.1给改仓库新增一个文件README,再检查文件状态

MacBook-Pro:mylibgit $ echo 'My Project' > README
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Untracked files:
  (use "git add <file>..." to include in what will be committed)


	README


nothing added to commit but untracked files present (use "git add" to track)
MacBook-Pro:mylibgit $

可以看到新增的文件README状态是Untracked的。

1.2 Tracking新文件

MacBook-Pro:mylibgit $ git add README
MacBook-Pro:mylibgit $

git add还可以指定一个文件夹,如果其指定添加的是文件夹则该文件夹下的所有文件都会被添加。

再检查文件状态

MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	new file:   README


MacBook-Pro:mylibgit $

可以看到新增的文件README状态变成staged的,表示其已经是tracked和staged的,下次commit会将其加入snapshot。

1.3 将修改的文件状态变为staged

修改一个已经被tracked的文件,然后检查下该文件的状态

MacBook-Pro:mylibgit $ vim CONTRIBUTING.md
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	new file:   README


Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)


	modified:   CONTRIBUTING.md


MacBook-Pro:mylibgit$

刚修改的文件CONTRIBUTING.md其状态是modified的处于"Changes not staged for commit"下,这表示被tracked的文件已经在工作目录下被修改了但是还未staged。

为了将修改后的内容在下一次commit中也加入snapshot,需要将其状态设为staged,可以使用git add命令将其stage。

MacBook-Pro:mylibgit $ git add CONTRIBUTING.md
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	modified:   CONTRIBUTING.md
	new file:   README


MacBook-Pro:mylibgit $

可以看到CONTRIBUTING.md目前已经是staged的了,会加入下一次的commit。

在commit之前,再次修改CONTRIBUTING.md文件,检查其状态

MacBook-Pro:mylibgit $ vim CONTRIBUTING.md
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	modified:   CONTRIBUTING.md
	new file:   README


Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)


	modified:   CONTRIBUTING.md


MacBook-Pro:mylibgit $

发现CONTRIBUTING.md现在既是staged又是unstaged的了。原来GIT stage文件的时候是在执行git add命令的时候,在git add之后再修改的内容是不会被加入下一次commit的。如果希望后修改的内容也加入下一次的commit,就必须再次执行git add将最新版本的文件设为staged的。

MacBook-Pro:mylibgit $ git add CONTRIBUTING.md
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is up-to-date with 'origin/master'.


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	modified:   CONTRIBUTING.md
	new file:   README


MacBook-Pro:mylibgit $

1.4 忽略不需要tracking的文件

有时我们不需要将某些文件加入GIT当中,同时又不希望git提示这些文件还未tracking,我们可以在.gitignore文件中设置哪些文件是我们希望被git忽略的。

MacBook-Pro:mylibgit $ cat .gitignore
/tests/clar.suite
/tests/clar.suite.rule
/tests/.clarcache
/apidocs
/trash-*.exe
/libgit2.pc
/config.mak
*.o
*.a
*.exe
*.gcda
*.gcno
*.gcov
.lock-wafbuild
.waf*
build/
build-amiga/
tests/tmp/
msvc/Debug/
msvc/Release/
*.sln
*.suo
*.vc*proj*
*.sdf
*.opensdf
*.aps
*.cmake
!cmake/Modules/*.cmake
.DS_Store
*~
.*.swp
tags
mkmf.log
MacBook-Pro:mylibgit $

.fitignore文件中可以出现的格式规则如下:

  • 空行或以#号开头的行会被忽略
  • 标准的glob文件匹配模式
  • 以/开头的模式不会进行递归匹配文件
  • 以/结尾的模式匹配的是一个目录
  • 以!开头的模式匹配的文件会被排除

Glob 是Unix-like的操作系统中用通配符指定文件的工具。*匹配的是0个或多个字符,?匹配一个字符,[abc]匹配一个在括号中的字符,[a-z]匹配一个在括号中指定范围中的字符。还可以使用两个星号匹配嵌套的目录,例如:a/**/z匹配的是a/z,a/b/z,a/b/c/z等等。

另一个.gitignore文件的例子:

  1 # no .a files
  2 *.a
  3
  4 # but do track lib.a, even though you're ignoring .a files above
  5 !lib.a
  6
  7 # only ignore the TODO file in the current directory, not subdir/TODO
  8 /TODO
  9
 10 # ignore all file in the build/ directory
 11 build/
 12
 13 # ignore doc/notes.txt, but not doc/server/arch.txt
 14 doc/*.txt
 15
 16 # ignore all .pdf files in the doc/ directory and any of its subdirectories
 17 doc/**/*.pdf

1.5 查看文件修改的具体细节

使用git diff命令查看做过哪些修改,它会展示文件中新增或删除了哪些行,哪些修改还未staged,哪些修改已经staged待下次commit时会添加进snapshot。

先修改README文件并stage,然后修改CONTRIBUTING.md文件不做stage,此时用git status命令查看

MacBook-Pro:mylibgit $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	modified:   README


Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)


	modified:   CONTRIBUTING.md


MacBook-Pro:mylibgit $

查看哪些修改是还未staged的,直接使用git diff命令,不加任何参数。这条命令将会用你工作目录下的文件与staging区域的文件做对比。

MacBook-Pro:mylibgit $ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 28a143570..4cc18f79c 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,3 +1,4 @@
+# add first line
 # Welcome to libgit2!


 We're making it easy to do interesting things with git, and we'd love to have
MacBook-Pro:mylibgit $

查看哪些修改是已经staged将加入下一次commit的,使用git diff --staged。这条命令将会用已经staged的修改和上一次commit的做对比。

MacBook-Pro:mylibgit $ git diff --staged
diff --git a/README b/README
index 56266d360..d3775bbb8 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
 My Project
+#add new line
MacBook-Pro:mylibgit $

1.6 提交修改

使用命令git commit提交修改,-v选项可以在默认的commit信息中加入详细修改的内容。

  1 
  2 # Please enter the commit message for your changes. Lines starting
  3 # with '#' will be ignored, and an empty message aborts the commit.
  4 #
  5 # On branch master
  6 # Your branch is ahead of 'origin/master' by 2 commits.
  7 #   (use "git push" to publish your local commits)
  8 #
  9 # Changes to be committed:
 10 #>------modified:   CONTRIBUTING.md
 11 #
 12 # ------------------------ >8 ------------------------
 13 # Do not touch the line above.
 14 # Everything below will be removed.
 15 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
 16 index 28a143570..4cc18f79c 100644
 17 --- a/CONTRIBUTING.md
 18 +++ b/CONTRIBUTING.md
 19 @@ -1,3 +1,4 @@
 20 +# add first line
 21  # Welcome to libgit2!
 22 -
 23  We're making it easy to do interesting things with git, and we'd love to have

退出编辑器时,Git会将修改提交,同时会保存提交时输入的信息

也可以选择在命令行中输入commit信息而不是打开编辑器编辑,使用-m flag:

MacBook-Pro:mylibgit $ git commit -m "commit inline"
[master 0f1ead64c] commit inline
 1 file changed, 1 insertion(+)
MacBook-Pro:mylibgit $

1.7 跳过staging区域直接提交所有的修改

在git commit命令中指定-a选项,Git会自动将所有已经tracked的文件的所有修改都先进行stage然后再commit,这可以使得我们不需要手动进行git add的操作,就可以直接将修改的内容进行提交

1.8 删除文件

使用git rm命令将文件从Git中删除,它会将指定的文件从staging区域中移除,同时也会从工作目录下移除。当下一次commit时,其将会消失并且不再被tracked。如果在git rm命令使用前该文件已经被修改并加入了staging区域,则必须使用-f进行强制移除,这样可以避免误移除了某些还未记录的数据。

MacBook-Pro:mylibgit $ git rm README
rm 'README'
MacBook-Pro:mylibgit $ ls
AUTHORS			COPYING			cmake			libgit2.pc.in
CHANGELOG.md		PROJECTS.md		deps			libgit2_clar.supp
CMakeLists.txt		README.md		docs			script
CODE_OF_CONDUCT.md	THREADING.md		examples		src
CONTRIBUTING.md		api.docurium		git.git-authors		tests
CONVENTIONS.md		appveyor.yml		include
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	deleted:    README


MacBook-Pro:mylibgit $

另一个有用的选项是--cached,可以使用它将staging区域的文件移除但保留工作目录下的文件。这将会在你忘记将某些文件加入.gitignore文件中且将其staged了,若想将它从Git tracking中移除但又要保留工作目录下的文件,则可以使用--cached。

git rm命令可以指定文件、目录或是glob匹配的任何文件,例如:

MacBook-Pro:mylibgit $ git rm log/\*.log

注意用于转义的反斜杠\。

1.9 移动文件

使用git mv命令移动文件

MacBook-Pro:mylibgit $ git mv file_form file_to

如果你将一个文件移动到其当前目录,但修改了它的名字,使用git status命令查看时,Git会认为你将这个文件重命名了。

MacBook-Pro:mylibgit $ git mv README.md README
MacBook-Pro:mylibgit $ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)


Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


	renamed:    README.md -> README


MacBook-Pro:mylibgit $

实际上,这相当于执行了以下命令

MacBook-Pro:mylibgit $ mv README.md README
MacBook-Pro:mylibgit $ git rm README.md
MacBook-Pro:mylibgit $ git add README






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值