Git 备忘清单

我每天都在使用 Git ,但是很多命令如果不是经常使用是很难记住的,于是整理了以下Git 命令清单,方便需要的时候查找。
持续更新中…

基础配置命令

  • 查看图形界面
    gitk --all&
    
  • 显示log信息
    git log --author=name  #显示某个作者的信息
    git log --graph --oneline --decorate --all #图形化显示
    git log --pretty=oneline  # 单行显示
    git log --name-status  #显示修改文件详情
    git shortlog # 显示按字母顺序排序的人名列表,以及他们对应的提交说明
    git shortlog -s -n  # 只显示每个开发者的 commit 数量
    git log --oneline --author="Paul Lewis" #单行显示"Paul Lewis"的所有commit
    git show 5966b66 #显示SHA=5966b66的commit信息
    git log --grep=bug #显示提到bug一词的commit
    
  • 查看隐藏目录.git
    ls -ah
    
  • Git config:
    git config --global color.ui true #彩色命令行显示
    git config format.pretty oneline #单行显示
    
  • 创建SSH key
    ssh-keygen -t rsa -C "youremail@example.com"
    cat ~/.ssh/id_rsa.pub
    
  • 如何避免每次输入密码:
    1. git config --global credential.helper cache
    2. 安装Git-Credential-Manager-for-Windows
      https://github.com/Microsoft/Git-Credential-Manager-for-Windows

Git 文件状态切换:

git本地操作

  • 不想追踪某个文件:

    1. Add the name to .git\info\exclude
    2. Add the name to .gitignore
  • 将改动加到staging区 stage

    git add .   #Stage all
    git add .gitignore   #stage .gitignore
    
  • 将改动移除staging区 unstage

    git reset HEAD api.c
    

    Paste_Image.png

  • 删除工作区的改动 discard

    git checkout -- api.c #discard changes in working directory
    

    Paste_Image.png

  • 删除追踪的文件 untracked

    git rm --cached api.c 
    

    Paste_Image.png

  • 恢复误删的文件

    git checkout -- api.c
    

    Paste_Image.png

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

  • 删除 untracked files
    git clean -f -n  #查看可以被删除的untracked files
    git clean -f
    

分支及远程操作

  • 推送到远程

    git remote add origin git@github.com:
    git push -u origin master
    
  • 新建分支dev

    git checkout -b dev
    
  • 推送分支

    git push origin <branch> 
    
  • 删除分支

    git checkout master
    git branch -d dev  
    
  • 删除远程分支

    git push origin --delete dev
    

版本切换

  • 退回之前的版本
    在Git中,用HEAD表示当前版本,上一个版本就是HEAD^, 上上一个版本就是HEAD^^,往上100个版本HEAD~100。

    git reset --hard HEAD^
    

    Paste_Image.png

  • 恢复删除的版本

    git reflog
    git reset --hard b6e2045
    

    Paste_Image.png

    Paste_Image.png

合并改动Merge

  • fast forward merge
    git checkout master
    git merge dev
    
  • 禁用Fast forward
    git merge --no-ff -m "merge with no-ff" dev  
    
  • 使用merge工具处理冲突
    git mergetool
    

    mergetool需要在.gitconfig 文件中配置,参见http://www.jianshu.com/p/f6deccbc8531

Submodule

  • 添加submodule
    git submodule add ssh://myrep.git
    
  • 删除submodule
    git submodule deinit -f -- submodule_folder/
    git rm -f submodule_folder/
    

Patch

  • 从某一个commit 生成patch

    git format-patch -1 <sha> --stdout > specific_commit.patch
    
  • 从当前HEAD生成包含x个commit的patch

    git format-patch -x --stdout > patch-ddmmyy.patch
    # where -x means how many commits back from the current head and it has to be integer.
    
  • 检查patch状态

    git apply --stat fix_bug.patch
    git apply --check fix_bug.patch
    
  • 打patch

    git am --signoff < fix_bug.patch
    

其他命令

  • 暂存未完成的改动

    git stash
    git stash list # 查看list
    git stash pop #恢复stash
    

    Paste_Image.png

  • 标签

    git tag V1.0 f1e9cf9 #添加
    git tag -d V0.1  #删除
    
  • 比较差异:

    git diff    #everything unstaged diffed to the last  commit
    git diff --cached  #everything staged diffed to the last commit
    git diff HEAD  #everything unstaged and staged diffed to the last commit
    
  • rebase 命令:

    git rebase -i HEAD~3 # 交互式地将 commit 变基到我们当前所在的 commit 向前三个的 commit
    git rebase origin/master # 在 master 后直接添加一个新版本,令提交历史更简洁
    

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值