git常用命令

git常用命令

本文是对廖雪峰老师的git笔记,算是消化过之后自我总结吧,排版可能稍显凌乱。。。

学习git的同学,强烈推荐廖老师的教程


  1. git init:把目录编程git可以管理的仓库,生成.git目录

  2. 把文件(的修改)添加到仓库,两步走

    commit之前,存在暂存区

git add readme.txt
git commit -m 'wrote a readme file'
  1. 查看当前仓库状态,其实就是比较上个版本的区别

       git status
       git diff HEAD -- readme.txt # 比较该文件上个提交版本的区别
    
  2. 查看提交记录:git log

  3. 回退到上一版本:git reset --hard HEAD^

  4. 回退到指定版本:git reset --hard commit_id

  5. 返回未来,查看未来的id:git reflog

  6. 丢弃修改:

    • 工作区的撤销:git checkout -- readme.txt #还没有add
    • 暂存区撤销:git reset HEAD readme.txt, git checkout -- readme.txt #还没有commit
    • 如果已经commit,直接用版本回退:git reset --hard HEAD commit_id
  7. 删除

    两种情况,如果确实想要删除:

    git rm test.txt, git commit -m 'remove test.txt'

    删错了想要恢复:git checkout --test.txt #不过只能恢复到最新版本

  8. 再github上创建repo之后,在本地的目录下运行:
    git remote add origin git@github.com:EricFH/learngit.git
    添加后,远程库的名字叫做origin
    然后把本地的master分支的内容推送到远程库
    git push -u origin master
    在第一次推送时加上-u(upstream),可以把本地的master和远程的master关联起来,方便
    从这以后,只要本地做了commit,可以推送到远程仓库:git push origin master

  9. 创建分支并切换

    git checkout -b dev
    # 等价于:git branch dev
    # git checkout dev
    

    切换之后可以正常再分支上做修改,并提交

  10. 查看当前分支:git branch

  11. 切回master,合并工作成果

    git checkout master
    git merge dev
    

    合并完之后可以删除dev分支了

    git branch -d dev
    
  12. 解决冲突

    $ git status
    On branch master
    Your branch is ahead of 'origin/master' by 2 commits.
      (use "git push" to publish your local commits)
    
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:   readme.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    <<<<<<< HEAD
    Creating a new branch is quick & simple.
    =======
    Creating a new branch is quick AND simple.
    >>>>>>> feature1
    

    修改冲突文件之后再git add readme.txt,然后git commit -m 'conflict fixed'

  13. 合并分支时,不使用fast forward模式,merge时,生成commit记录

    git merge --no-ff -'merge with no-ff' dev

    这样就可以查看合并这件事情的历史

  14. 暂时保存工作现场

    git stash一下,把结果放到栈里;再git stash pop,回到工作现场。

  15. 多人合作模式:

    • 查看远程库信息,使用git remote -v
    • 本地新建的分支如果不推送到远程,对其他人就是不可见的;
    • 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
    • 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
    • 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name
    • 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。
    • 如果合并有冲突,则解决冲突,并在本地提交;
    • 没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!
  16. 给重要的commit加上tag作为额外信息

    • 命令git tag <tagname>用于新建一个标签,默认为HEAD,也可以指定一个commit id;
    • 命令git tag -a <tagname> -m "blablabla..."可以指定标签信息;
    • 命令git tag可以查看所有标签。
  17. 本地-远端处理tag

    • 命令git push origin <tagname>可以推送一个本地标签;
    • 命令git push origin --tags可以推送全部未推送过的本地标签;
    • 命令git tag -d <tagname>可以删除一个本地标签;
    • 命令git push origin :refs/tags/<tagname>可以删除一个远程标签。
  18. 忽略一些文件(其实也可以不管,但是每次git status都显示untracked files ...),所以直接在根目录下写.gitignore文件即可

    • 忽略某些文件时,需要编写.gitignore
    • .gitignore文件本身要放到版本库里,并且可以对.gitignore做版本管理!
  19. 别名,可以自定义一些alias,看到比较有意思的一个:

    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    
  20. 全局配置记录在~/.gitconfig内,可以直接改文件开代替git config --global color.ui true之类的

  21. 项目的局部配置位置:.git/config

posted @ 2018-09-14 19:56  潇雨危栏  阅读(218)  评论(0编辑  收藏  举报