zoukankan      html  css  js  c++  java
  • GIt学习笔记

    1、安装GIt

    1.1、Win下安装git:

    下载:https://git-scm.com/download/win

    基本上一路回车,安装成功后,点击Git Bash,出现如下图说明安装成功:

    1.2、设置名字和邮件:

    git config --global user.name ldq
    git config --global user.email ”ldq@qq.com“
    

    2、创建版本库

    版本库又名仓库,英文名repository

    小结:

    git init   #初始化git

    git add  file1 

    git commit -m “注释”  #提交版本信息

    Administrator@WSQRUUOEZ1H434O MINGW64 ~
    $ mkdir learngit#创建版本库文件
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~
    $ cd learngit/
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit
    $ pwd
    /c/Users/Administrator/learngit
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit
    $ git init#初始化版本库文件
    Initialized empty Git repository in C:/Users/Administrator/learngit/.git/
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ ls -al#多了一个.git文件
    total 20
    drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 ./
    drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 ../
    drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 .git/
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ vi readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git add readme.txt#新增文件
    warning: LF will be replaced by CRLF in readme.txt.
    The file will have its original line endings in your working directory
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ ls
    readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git commit -m "wrote a readme file"#提交代码-m注释
    [master (root-commit) bcb8042] wrote a readme file
     1 file changed, 2 insertions(+)
     create mode 100644 readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $
    

    3、时光机穿梭

    小结:

    git status  #查看git状态

    git add  file1 #新增代码文件

    git diff readme.txt #比较代码文件

    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ vi readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ 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")
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git add readme.txt
    warning: LF will be replaced by CRLF in readme.txt.
    The file will have its original line endings in your working directory
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   readme.txt
    
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git commit -m "test readme.txt"
    [master ba06e36] test readme.txt
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git diff readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ vi readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git diff readme.txt
    warning: LF will be replaced by CRLF in readme.txt.
    The file will have its original line endings in your working directory
    diff --git a/readme.txt b/readme.txt
    index c9d14fd..4aed2d2 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,2 @@
    -Git is a test version control system.
    +Git is a test2 version control system.
     Git is free software.
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ 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")
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git add readme.txt
    warning: LF will be replaced by CRLF in readme.txt.
    The file will have its original line endings in your working directory
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git commit -m "test2 readme.txt"
    [master 0908706] test2 readme.txt
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $
    

    1、版本回退

    git用hard来指定那个版本,使用git reset来指定回退那个版本

    git reset --hard HEAD^  #指定会回到上一个版本 上上个版本^^  上10个版本~10

    git reset  --hard 0809  #指定版本号

    git log   #查看版本信息

    git log  --pretty=oneline #美化查看信息

    git reflog  #查看历史信息

    Administrator@WSQRUUOEZ1H434O MINGW64 ~
    $ cd learngit/
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git log
    commit 09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master)
    Author: ldq <“961811769@qq.com>
    Date:   Tue Sep 25 17:32:05 2018 +0800
    
        test2 readme.txt
    
    commit ba06e367f0b657030314918fe5e0249dafe89880
    Author: ldq <“961811769@qq.com>
    Date:   Tue Sep 25 17:30:30 2018 +0800
    
        test readme.txt
    
    commit bcb80425fd26bda94e289953d4c552f3ba1a1c02
    Author: ldq <“961811769@qq.com>
    Date:   Tue Sep 25 17:18:55 2018 +0800
    
        wrote a readme file
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git log --pretty=oneline
    09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master) test2 readme.txt
    ba06e367f0b657030314918fe5e0249dafe89880 test readme.txt
    bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git reset --hard HEAD^
    HEAD is now at ba06e36 test readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git log --pretty=oneline
    ba06e367f0b657030314918fe5e0249dafe89880 (HEAD -> master) test readme.txt
    bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ cat readme.txt
    Git is a test version control system.
    Git is free software.
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git reset --hard 0908
    HEAD is now at 0908706 test2 readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git log --pretty=oneline
    09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master) test2 readme.txt
    ba06e367f0b657030314918fe5e0249dafe89880 test readme.txt
    bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ cat readme.txt
    Git is a test2 version control system.
    Git is free software.
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git reflog
    0908706 (HEAD -> master) HEAD@{0}: reset: moving to 0908
    ba06e36 HEAD@{1}: reset: moving to HEAD^
    0908706 (HEAD -> master) HEAD@{2}: commit: test2 readme.txt
    ba06e36 HEAD@{3}: commit: test readme.txt
    bcb8042 HEAD@{4}: commit (initial): wrote a readme file
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $
    

    2、工作区和暂存区

    Git和其他SVN不同之处在于还有一个暂存区的概要。

    工作区:看到的文件夹eg:learngit

    版本库:learngit 下有个隐藏文件.git的文件夹是Git版本库

    暂存区:stage或者index #git add file1 file2 后就把file1和file2  存放到了stage

    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ vim readme.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ vim test.txt
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ 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
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            test.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git add readme.txt  test.txt
    warning: LF will be replaced by CRLF in test.txt.
    The file will have its original line endings in your working directory
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ git commit -m "add-test.txt-index"
    [master 47759dc] add-test.txt-index
     2 files changed, 2 insertions(+)
     create mode 100644 test.txt
    

    3、管理修改

    Git比其他版本控制系统设计的优秀,因为Git跟踪并管理的是修改,而非文件。

    如果第一次修改完执行git add 然后再对他修改,修改完了,直接执行git commit ,提交的是第一次修改完的内容,而非第二次修改的,所以第二次修改完以后,需要再次执行add操作。

    第一次修改 -> git add -> 第二次修改 -git add -> git commit

    4、撤销修改

    敲代码时候,修改错文件了,想把这个文件的修改给撤销了,应该怎么办:

    1、git checkout -- file      #没有执行过git add 的时候

    2、git reset HEAD <file>  #执行过git add的时候

          git checkout -- file  

    3、git reset --hard HEAD^  #执行过commit

    5、删除文件

    rm test.txt

    git rm test.txt  #git add test.txt 效果一样

    git commit -m "remove test.txt" 

    删除错了想恢复怎么办?

    git checkout -- test.txt   

    git checkout       #用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

    3、添加远程库 

    1、新建本地仓库

    这里用之前做实验的,就不在新建了,新建方法如下:

    echo "# test" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin git@github.com:ldqchw/test.git
    git push -u origin master
    

    2、本地创建SSH公钥

    ssh-keygen -t rsa -C "ldqchw"

    -C   "GItHub用户名"

    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ ssh-keygen -t rsa -C "ldqchw"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
    Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:2Ts7qFAQNiJyKd0Joos+eu4ZKfRAWgXWHlagErsfaHg ldqchw
    The key's randomart image is:
    +---[RSA 2048]----+
    |=.***+.          |
    |+Bo=*o           |
    |+ooo..           |
    |=*  ..   o       |
    |B+E   . S .      |
    |+ooo .     .     |
    |.o+..    .o      |
    |..oo .  . .o     |
    |.++   ..  ..     |
    +----[SHA256]-----+
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ cat /c/Users/Administrator/.ssh/
    id_rsa       id_rsa.pub   known_hosts
    
    Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
    $ cat /c/Users/Administrator/.ssh/id_rsa.pub
    damaXXXXXXXXXXXXXXXXXXXXXdama
    

    3、将SSH公钥导入GitHub

    4、GitHub上新建一个项目

    5、本地同步到远程

    第一次同步:

    git remote add origin git@server-name:path/repo-name.git  或者  git remote add origin https://github.com/ldqchw/learngit.git

    eg:

    git remote add origin https://github.com/GitHub名字/仓库名字.git

    ssh -T git@github.com #测试是否能通信

    git push -u origin master

    以后同步:

    git push  origin master

    origin  #俗称默认远程可改

    6、从远程克隆到本地

    git clone git@github.com:michaelliao/gitskills.git 

    参考:

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

    https://www.cnblogs.com/qcwblog/p/5709720.html

  • 相关阅读:
    Alpha冲刺(8/10)
    Alpha冲刺(7/10)
    Alpha冲刺6
    Alpha冲刺5
    GIT团队实战博客
    Alpha冲刺4
    STM32和WM8960 I2S 利用DMA双缓冲音频播放和录音(二)
    STM32和WM8960 I2S 利用DMA双缓冲音频播放和录音(一)
    USART DMA双缓冲给PC发送数据和接收PC数据
    详细理解STM32F42x系列的DMA配置
  • 原文地址:https://www.cnblogs.com/zhuxr/p/9700920.html
Copyright © 2011-2022 走看看