美文网首页
git使用基础篇

git使用基础篇

作者: 逍遥无铭 | 来源:发表于2019-03-27 11:50 被阅读0次

    转自: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

    相关文章

      网友评论

          本文标题:git使用基础篇

          本文链接:https://www.haomeiwen.com/subject/kctzvqtx.html