Git Commands

229 阅读3分钟

git reflog

显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git log只包括当前分支的commit.

查找branch history

  • git reflog |grep “branch name"

显示本地分支和服务器分支的映射关系

  • git branch -vv # 显示本地分支和服务器分支的映射关系
  • git log --graph --decorate
  • git log --graph --decorate --simplify-by-decoration

git查看各个branch之间的关系图

  1. 使用git log命令

git log --graph --decorate --oneline --simplify-by-decoration --all

说明:

--decorate 标记会让git log显示每个commit的引用(如:分支、tag等)

--oneline 一行显示

--simplify-by-decoration 只显示被branch或tag引用的commit

--all 表示显示所有的branch,这里也可以选择,比如我指向显示分支ABC的关系,则将--all替换为branchA branchB branchC

  1. 使用gitk工具

gitk --simplify-by-decoration --all

查找文件修改记录

git log --pretty=oneline 文件路径

git show 《commit-tag》 文件路径

查找远程branch状态

  • git branch -a #列出所有的branch包括远程
  • 远程分支为:remotes/origin/remote-branch

查看远程分支log: git log remotes/origin/remote-branch

  • git remote show origin #查看远程分支状态

状态: stale (use 'git remote prune' to remove)

这种情况远程分支已经删除,用下面命令可以清理本地遗留的branch

git remote prune origin

git remote show origin
* remote origin
  Fetch URL: http://xxxx.git
  Push  URL: http://xxxx.git
  HEAD branch: develop
  Remote branches:
    develop                                                                                      tracked
    bugfix/AX-3312-implement-the-inclusion-of-document-type                                      tracked
    feature/AX-3313-unit-testing                                                                 new (next fetch will store in remotes/origin)
    feature/Sample_2_0_Develop                                                                   tracked
    feature/AX-3589-Book-API                                                                     tracked
    master                                                                                       tracked
    refs/remotes/origin/feature/AX-3018-Document Inventory                                       stale (use 'git remote prune' to remove)
    release/Sample_2_0_Develop                                                                   tracked
  Local branches configured for 'git pull':
    develop                       merges with remote develop
    feature/Sample_2_0_Develop    merges with remote feature/Sample_2_0_Develop
    feature/AX-3589-Book-API      merges with remote feature/AX-3589-Book-API
    
  Local refs configured for 'git push':
    develop                       pushes to develop                   (local out of date)
    feature/Sample_2_0_Develop  pushes to feature/Sample_2_0_Develop  (local out of date)
    feature/AX-3589-Book-API      pushes to feature/AX-3589-Book-API  (local out of date)

Git branch Opeartion

Head Branch 详解

Git Fetch/Pull/Merge/Remote详解

Git Merge/Rebase区别 Git Merge/Rebase区别2

git remote -v #列出远程分支

Git Tag Operation

执行完merge操作后回退

git reflog

查看merge操作的上一个提交记录的版本号

git reset –hard 版本号

这样可以回滚到merge之前的状态

git如何删除已经 add 的文件 (如何撤销已放入缓存区文件的修改)

git add 添加 多余文件 这样的错误是由于, 有的时候 可能

git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件

git add 如果添加了错误的文件的话

撤销操作

  • git status 先看一下add 中的文件
  • git reset HEAD 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了
  • git reset HEAD XXX/XXX/XXX.java 就是对某个文件进行撤销了

git查看文件改动记录

  • git diff commit-id1 commit-id2 --stat

看两个版本之间有哪些文件改动

  • git diff commit-id1 commit-id2 看具体文件改动
  • git diff branch1 branch2 --stat

看两个分支之间有哪些文件差异

  • git diff tag1 tag2 --stat

看两个tag之间有哪些文件差异或者改动

  • git log file

看一个文件的改动,以commit的形式展现

  • git log -p file

看具体一个文件的历史改动记录

Trouble Shooting

  • Your configuration specifies to merge with the ref 'refs/heads/xxxx' from the remote, but no such ref was fetched

    • 切换到主分支(或者被依赖的分支,也就是你从哪个分支上拉取新的分支),运行:

      • git pull
      • git fetch -p

        等于git fetch -prune

        • fetch同时在本地删除远程不存在的branch
    • how to solve and recover deleted branch

Renaming branches remotely in Git

  • $ git branch new-branch-name origin/old-branch-name
  • $ git push origin --set-upstream new-branch-name
  • $ git push origin :old-branch-name

Then, to see the old branch name, each client of the repository would have to do:

  • $ git fetch origin
  • $ git remote prune origin