git使用基础篇

转自:https://my.oschina.net/foreverich/blog/1921542

1. 如何创建一个git本地仓库?

# shell $> mkdir mygitdir

# shell $> cd mygitdir

# shell $> git init

Initialized empty Git repository in /Users/foreverich/git/mygitdir/.git/

此时会在mygitdir目录下创建一个.git的隐藏目录, 用于存储本地版本信息

2. 如何本地提交一个文件?

# shell $> echo 'git test' > readme.md

# shell $> git add readme.md

# shell $> git commit -m "add readme.me for test"

[master (root-commit) ca3c109] add readme.me for test

1 file changed, 1 insertion(+)

create mode 100644 readme.md

[master (root-commit) ca3c109] add readme.me for test

1 file changed, 1 insertion(+)

create mode 100644 readme.md

3. 查看git当前状态

# shell $> git status

On branch master

nothing to commit, working tree clean

# shell $> echo 'git new line' >> readme.md

# shell $> git status

On branch master

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:  readme.md

no changes added to commit (use "git add" and/or "git commit -a")

可以看到我们修改readme.md文件后, git status的状态变更了,说有未提交的内容

4. 查看git的具体变更内容git diff

# shell $> git diff

diff --git a/readme.md b/readme.md

index f6edd6e..6e1edee 100644

--- a/readme.md

+++ b/readme.md

@@ -1 +1,2 @@

git test

+git new line

特别注意: 此时是交互式的, 退出按q即可, 如果不想进入交互模式, 则添加如下选项: --exit-code

# shell $> git diff --exit-code

diff --git a/readme.md b/readme.md

index f6edd6e..6e1edee 100644

--- a/readme.md

+++ b/readme.md

@@ -1 +1,2 @@

git test

+git new line

5. 再次提交修改的内容

# shell $> git add readme.md

# shell $> git commit -m "add new content to readme.md"

[master 5230322] add new content to readme.md

1 file changed, 1 insertion(+)

# shell $> git status

On branch master

nothing to commit, working tree clean

可以发现再次提交后, git status提示版本仓库目录是干净的

6. 查看提交历史

# shell $> git log

commit 52303226fadd453f35f82fe793f40746fb814511 (HEAD -> master)

Author: foreverich <xxxxxx@example.com>

Date:  Tue Jul 31 22:37:12 2018 +0800

    add new content to readme.md

commit ca3c109e0f6d8738af7918b42cc4e6b0f0c7d5df

Author: foreverich <xxxxxx@example.com>

Date:  Tue Jul 31 22:19:15 2018 +0800

    add readme.me for test

可以看到我们提交了2次, 以及commit的hash值, 作者, 提交时间, 2次的commit内容

7. 对比git两次提交的差异

使用命令git diff hash1 hash2对比两次提交之间的差异

# shell $> git diff ca3c109e0f6d873 52303226fadd453f3

diff --git a/readme.md b/readme.md

index f6edd6e..6e1edee 100644

--- a/readme.md

+++ b/readme.md

@@ -1 +1,2 @@

git test

+git new line

git diff只检查工作区和暂存区或本地仓库之间的差异

如果要比较暂存区与本地仓库之间的差异, 则需要使用 git diff --cached 指令

8. 在本地回退到上个版本

# shell $> git reset --hard HEAD^

HEAD is now at ca3c109 add readme.me for test

# shell $> cat readme.md

git test

HEAD表示当前版本, HEAD^表示上个版本, HEAD^^表示上2个版本, HEAD~100表示倒数第100个版本

也可以使用hash值来表示要退到的版本, 甚至刚刚消失的版本, 我们还可以撤退回来.

# shell $> git reset --hard 52303226fadd453f3

HEAD is now at 5230322 add new content to readme.md

# shell $> cat readme.md

git test

git new line

9. 如果你找不到git版本的hash,可以通过 git reflog查找

# shell $> git reflog

5230322 (HEAD -> master) HEAD@{0}: reset: moving to 52303226fadd453f3

ca3c109 HEAD@{1}: reset: moving to HEAD^

5230322 (HEAD -> master) HEAD@{2}: commit: add new content to readme.md

ca3c109 HEAD@{3}: commit (initial): add readme.me for test

10. 修改文件后, 还没有git add到暂存区撤销方法

# shell $> sed -i 's/new/second/g' readme.md

# shell $> echo 'the third line' >> readme.md

# shell $> cat readme.md

git test

git second line

the third line

# shell $> git status

On branch master

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:  readme.md

no changes added to commit (use "git add" and/or "git commit -a")

## 撤销文件的修改

# shell $> git checkout -- readme.md

## 再次查看文件内容, 会发现刚刚的修改已经撤销了

# shell $> cat readme.md

git test

git new line

git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令

11. 修改文件后, 已经git add到暂存区的撤销方法

# shell $> echo 'git add readme line' >> readme.md

# shell $> git add readme.md

# shell $> git status

On branch master

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

    modified:  readme.md

## 上面已经提示我们可以通过 git reset HEAD <file> 撤销unstage状态

# shell $> git reset HEAD readme.md

Unstaged changes after reset:

M  readme.md

## 此时表示从暂存区取消 git add 操作, 但是我们的修改还是存在的.

# shell $> cat readme.me

git test

git new line

git add readme line

## 要想把这个修改撤销掉, 那就只要参考前面的 git checkout -- readme.md 操作

# shell $> git checkout -- readme.md

## 再次查看文件内容, 会发现刚刚的修改已经撤销了

# shell $> cat readme.md

git test

git new line

## 上面的命令也可以合并为一个命令来执行,不需要先 `git reset` 再 `git checkout`

# shell $> git reset --hard

## 可以一步到位地把你的修改完全恢复到未修改的状态

12. 修改文件后, 已经 git add 并且 git commit, 但是还没有 git push 推送到远程, 怎么撤销

# shell $> git reset --hard origin/master

这里 origin/master 表示回退到远程的master分支状态

13. 修改文件后, 已推送到远程, 怎么撤销

假如你既git add了,又git commit了,并且还git push了,此时你的代码已经进入远程仓库。如果想恢复的话,你只需要先恢复本地仓库,再强制push到远程仓库就可以了. 因为你的本地仓库和远程仓库是等价的.

# shell $> git reset --hard HEAD^

# shell $> git push -f

14. 删除版本库里的一个文件

首先, 先在版本库里新增一个文件

# shell $> echo "new file" > new.txt

# shell $> git status

On branch master

Untracked files:

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

    new.txt

nothing added to commit but untracked files present (use "git add" to track)

# shell $> git add new.txt

# shell $> git commit -m "add new file"

[master 0bd592b] add new file

1 file changed, 1 insertion(+)

create mode 100644 new.txt

然后, 我们尝试删除

# shell $> rm new.txt

# shell $> git status

On branch master

Changes not staged for commit:

  (use "git add/rm <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    new.txt

no changes added to commit (use "git add" and/or "git commit -a")

此时, 提示我们文件被删除了, 但是版本库里还有这个文件, 要从版本库里删除, 应该使用如下命令

# shell $> git rm new.txt

rm 'new.txt'

# shell $> git status

On branch master

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

    deleted:    new.txt

# shell $> git commit -m "delete some file"

[master dd13a7a] delete some file

1 file changed, 1 deletion(-)

delete mode 100644 new.txt

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容