Summary of git introductory tutorial

git distributed version control system

content

  • Install
  • Create a repository
  • Add files to repository
  • View Workspace Status and Modifications to Content
  • Rolling back the historical version
  • Workspace and Staging Area
  • Undo changes
  • Delete Files
  • Associate a GitHub repository
  • Create and merge branches
  • branch is not cleared
  • bug branch
  • drop branch
  • Create labels
  • Label operation
  • Associate GitHub and Code Cloud at the same time
  • Install

    Xcode
  • Create a repository

    $ mkdir learngit Create the learngit directory
    $ cd learngit
    $ pwd Display the current directory
    /Users/michael/learngit
    Step 2
    git init Turn the directory into a repository that git can manage
    If you don't see the .git directory, it's because this directory is hidden by default Yes, you can see it with the ls -ah command
  • Add files to repository

    1. Create a file, such as readme.txt, in the learnngit directory.
    2.git add (file), add to the warehouse
    3.git commit -m " ", the description of this submission
  • View Workspace Status and Modified Content

    git status
    git diff
  • Rolling back the historical version

    git log (git log --pretty=oneline). View historical files
    git reset --hard HEAD^ HEAD current file, HEAD^ previous historical file, HEAD^^ previous historical file
    git reset --hard id Back by ID A certain version, used to cat file from past to future
    . View file
    git reflog. View history command (ID number to go back to the future)
  • Workspace and Staging Area

    工作区:能看到的目录,如learngit文件夹
    版本库:隐藏的目录 .git。暂存区storage 分支master
    git add把文件添加进去,实际上就是把文件修改添加到暂存区;
    git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支、
  • 撤销修改

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout – file。
    场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
    场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考历史版本回退,不过前提是没有推送到远程库。
  • 删除文件

    从版本库中删除(不能恢复)
    $ git rm test.txt
    rm ‘test.txt’
    $ git commit -m "remove test.txt"
    手动删错了文件,从版本库恢复
    $ git checkout – test.txt
  • 关联GitHub库

    1.创建SSH Key
    $ ssh-keygen -t rsa -C "[email protected]
    一路回车。此时在用户主目录里找到.ssh目录,里面有id_rsa 私钥和id_rsa.pub 公钥两个文件
    2.登陆GitHub—>setting—->SSH Keys
    title任意,在Key文本框里粘贴id_rsa.pub文件的内容
    3.GitHub,创建新仓库repo
    4.关联$ git remote add origin [email protected]:path/repo-name.git
    第一次推送git push -u origin master
    之后推送git push origin master

  • 创建与合并分支

    创建分支 ,名称dev: $ git checkout -b dev
    git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
    $ git branch dev
    $ git checkout dev
    git branch命令查看当前分支,当前分支前面会标一个*号
    可以在分支修改文件,提交
    切换回master分支:
    $ git checkout master
    Switched to branch 'master’
    将dev分支的工作合并到master:
    $ git merge dev
    删除分支:
    $ git branch -d dev
    总结:
    查看分支:git branch
    创建分支:git branch 
    切换分支:git checkout 
    创建+切换分支:git checkout -b 
    合并某分支到当前分支:git merge 
    删除分支:git branch -d

  • 分支不清除

    合并dev分支,请注意–no-ff参数,表示禁用Fast forward:
    $ git merge --no-ff -m “merge with no-ff” dev
  • Bug分支

    修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
    当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。
  • 丢弃分支

    丢弃一个没有被合并过的分支,可以通过git branch -D 强行删除
  • 创建标签

    • 命令git tag 用于新建一个标签,默认为HEAD,也可以指定一个commit id;

    • git tag -a -m "blablabla…"可以指定标签信息;

    • git tag -s -m "blablabla…"可以用PGP签名标签;

    • 命令git tag可以查看所有标签。

  • 操作标签

    • 命令git push origin 可以推送一个本地标签;

    • 命令git push origin --tags可以推送全部未推送过的本地标签;

    • 命令git tag -d 可以删除一个本地标签;

    • 命令git push origin :refs/tags/可以删除一个远程标签。

  • 同时关联GitHub和码云

    1.在码云上创建一个新项目,跟GitHub操作一样
    2.先取消已在GitHub关联的名为origin的远程库
    git remote rm origin
    3.关联GitHub远程库(名称不同 github)
    git remote add github [email protected]:xxx/learngit.git
    4.再关联码云的远程库 名称gitee
    git remote add gitee https://gitee.com/xxx/learngit.git
    5.查看远程库信息。
    git remote -v
    6.推送到GitHub
    git push github master
    7.推送到码云
    git push gitee master

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324498261&siteId=291194637