您的位置:首页 > 其它

Git基础-获取仓库、提交、查看历史、撤销

2017-02-06 00:00 417 查看
摘要: 简单说明如何获取git仓库,提交,查看历史,撤销这4个部分的操作

1 获取git仓库

有两种取得git仓库的方法:

在现有目录中初始化仓库

$ git init


另一种是克隆现有仓库

$ git clone https://github.com/libgit2/libgit2 $ git clone https://github.com/libgit2/libgit2 mylibgit

第二种会在本地建一个mylibgit的仓库名

2 记录每次更新到仓库

文件暂存

git add,意思是“添加内容到下一次提交中”,这是一个多功能命令

开始跟踪新文件

将已跟踪的文件放到暂存区

合并时,把有冲突的文件标记为已解决状态

状态简览

git status -s:查看文件状态

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

M靠右:表示文件修改,但未放到暂存区

M靠左:表示文件已修改,并且已放到暂存区

忽略文件

.gitignore 文件记录忽略的文件

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf


查看已暂存和未暂存的修改

git diff:比较工作目录中当前文件和暂存区快照之间的差异,也就是修改之后还没有暂存的变化内容

git diff --staged:查看已暂存的,将要添加到下次提交里的

提交更新

git commit:提交记录的是放在暂存区的快照,以后可以回到这个状态,或比较

跳过使用暂存区

git commit -a:自动将已跟踪的文件暂存起来一并提交,从而跳过git add 步骤

移除文件

git rm: 从跟踪文件清单(确切说是暂存区)中移除,并连带从工作目录中删除

git rm -f:强制删除,如果文件修改已经放到暂存区

git rm --cached fileName:文件保留在磁盘,git不再跟踪

移动文件

git mv file_from file_to:重命名

3 查看提交历史

git log

git log -p -2:显示最近两次提交的内容差异

git log --stat:显示每次提交的简略统计信息

git log --pretty=oneline:oneline表示每个提交放在一行显示,另外还有full, fuller, short

git log --pretty=format:"%h - %an, %ar : %s":定制要显示的记录格式

下面的例子显示了提交对象的短哈希,作者名字,修订日期,提交说明

ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit

添加ASCII字串形象展示分支、合并历史:

$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
*  5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
*  11d191e Merge branch 'defunkt' into local

查看最近两周的提交, 2008年10月1日后的提交

$ git log --since=2.weeks
$ git log --since="2008-10-01"










4 切分支

git checkout -b [分支名] [远程名]/[分支名]

git checkout --track origin/serverfix (git 1.6.2以上)

本地分支名为sf,不同于远程分支名

git checkout -b sf origin/serverfix

5 push到远程仓库

git push (远程仓库名) (分支名)

比如:git push origin serverfix

Git 自动把 serverfix 分支名扩展为 refs/heads/serverfix:refs/heads/serverfix ,意为“取出我在本地的 serverfix 分支,推送到远程仓库的 serverfix 分支中去”。

也可以运行:git push origin serverfix:serverfix

若想把远程分支叫作 awesomebranch,可以用 git push origin serverfix:awesomebranch 来推送数据

6 撤销操作

修改提交(比如,有漏提交的文件)

git commit --amend:提交暂存区的文件,如果文件未改变,那么修改的只是提交信息,最终,第二次的提交会替代第一次的提交

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit -m 'initial commit' --amend


取消暂存的文件(取消git add 的文件)

git reset HEAD <file>

$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed:    README.md -> README
modified:   CONTRIBUTING.md


撤销对文件的修改

git checkout -- <file>

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

$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed:    README.md -> README


参考资料:https://git-scm.com/book/zh/v2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git 基础