• git学习


    创建ssh key

    $ ssh-keygen -t rsa -C "youremail@example.com"
    
    • 1

    更新仓库

    $ git push origin master
    
    • 1

    新建/修改文件

    新建文件和修改文件都是在git仓库目录下进行的,如果是新建就将新建的文件放到git仓库目录,修改则直接在文件上进行修改,例如:

    我们编写一个readme.txt文件,内容如下:

    Git is a version control system.
    Git is free software.
    
    • 1
    • 2

    让后放到git目录下,然后使用git add指令添加,修改也是这个指令

    $ git add readme.txt
    
    • 1

    然后用commit提交修改

    $ git commit -m "wrote a readme file"
    
    • 1

    这时我们对readme文件进行修改

    Git is a distributed version control system.
    Git is free software.
    
    • 1
    • 2

    然后可以用git status查看状态

    $ 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.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    上面的输出告诉我们,readme被修改了,但是还没提交
    我们可以用git diff查看修改了什么

    $ git diff readme.txt 
    diff --git a/readme.txt b/readme.txt
    index 46d49bf..9247db6 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,2 @@
    -Git is a version control system.
    +Git is a distributed version control system.
     Git is free software.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    然后我们重复之前的操作就可以提交修改了

    $ git add readme.txt
    $ git commit -m "add distributed"
    
    • 1
    • 2

    这时我们再进行修改,将readme改为

    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    
    • 1
    • 2

    然后提交

    $ git add readme.txt
    $ git commit -m "append GPL"
    
    • 1
    • 2

    查看log

    使用git log命令可以查看历史修改信息

    $ git log
    commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 21:06:15 2018 +0800
    
        append GPL
    
    commit e475afc93c209a690c39c13a46716e8fa000c366
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 21:03:36 2018 +0800
    
        add distributed
    
    commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 20:59:18 2018 +0800
    
        wrote a readme file
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    使用–pretty=oneline减少输出信息

    $ git log --pretty=oneline
    1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
    e475afc93c209a690c39c13a46716e8fa000c366 add distributed
    eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
    
    • 1
    • 2
    • 3
    • 4

    前面的长串是版本号
    HEAD表示最新版本,HEAD^ 表示上一个版本,HEAD^^ 表示上上个版本,往上很多版本比如100可以写为HEAD~100

    版本回退

    使用git reset 可以实现版本回退

    $ git reset --hard HEAD^
    
    • 1

    这时我们再看log就会变化了

    $ git log
    commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 21:03:36 2018 +0800
    
        add distributed
    
    commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 20:59:18 2018 +0800
    
        wrote a readme file
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如果我们想反悔的话,可以使用git reflog查看之前的操作,找到想要返回的版本号就行

    $ git reflog
    e475afc HEAD@{1}: reset: moving to HEAD^
    1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
    e475afc HEAD@{3}: commit: add distributed
    eaadf4e HEAD@{4}: commit (initial): wrote a readme file
    $ git reset --hard 1094a
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    丢弃工作区的修改

    使用 git checkout

    $ git checkout -- readme.txt
    
    • 1

    丢弃暂存区的修改

    $ git reset HEAD readme.txt
    
    • 1

    文件的删除

    $ git rm test.txt
    $ git commit -m "remove test.txt"
    
    • 1
    • 2
  • 相关阅读:
    vue 鼠标划入划出多传一个参数
    复现urlcode编码绕过xss限制两个demo
    第64步 深度学习图像识别:多分类建模误判病例分析(Pytorch)
    Stable Diffusion【应用篇】【图片修复】:模糊头像照片的高清修复
    数学工程学|正态分布及其图形
    南大通用GBase8s 常用SQL语句(263)
    Linux-常用命令(持续更新中)
    GO-SLAM——论文简析
    步进电机的使用方法和控制方式基本介绍
    VUE基础的一些实战总结
  • 原文地址:https://blog.csdn.net/qq_34688283/article/details/125616119