Skip to content
William Zhang edited this page Jan 16, 2019 · 2 revisions

Git

http://www.cheat-sheets.org/#Git

git-gui

$ sudo yum install git-web
$ git gui ...

gitk

$ sudo yum install gitk
$ gitk

gitweb

$ sudo yum install gitweb
$ man gitweb
$ sudo yum install lighttpd
$ cd ~/github/misc
$ git instaweb

http://git-scm.com/book/

branch

$ git checkout -b dev-review origin/dev
$ git branch <your branch> // Switch to branch
$ git pull origin master // Sync master changes inside your branch
$ git diff --name-only master <branch> // List of the changed files

log

$ git log --merges

diff

怎么diff当前branch与它的parent(以找出当前branch的所有改动。先找到它的parent的最后一次提交对应的hash值。

$ git log | more 

再做diff

$ git diff <hash> HEAD

怎么diff给定文件的两个版本

先找到文件的修改历史

$ git log sql/sys_vars.cc

从中找到两个待比较的hash值,再开始比较

$ git diff hash1 hash2 -- sql/sys_vars.cc

** 在有未决冲突时,怎么回滚掉这些未决的文件?

例如:
#+NAME: <s>
$ git checkout master
sql/field.cc: needs merge
sql/sql_show.cc: needs merge
sql/sql_show.h: needs merge
sql/sql_yacc.yy: needs merge
error: you need to resolve your current index first
$ git reset --merge

删除未受控的本地代码

$ git clean -f
But beware... there's no going back. Use -n or --dry-run to preview the damage you'll do.

撤销最近的一个commit

$ git reset --hard HEAD~1
The HEAD~1 means the commit before head.

撤销到指定的一个commit

$ git reset --hard <sha1-commit-id>
  • 如果已经提交到了远程,则还需要push一下

If you already pushed it, you will need to do a force push to get rid of it…

$ git push origin HEAD --force

rollback

git revert <hash> git reset HEAD Makefile

clang-format and git

https://mirrors.edge.kernel.org/pub/software/scm/git/docs/gitattributes.html

From a clean working directory:

$ echo "* text=auto" >.gitattributes
$ git add --renormalize .
$ git status # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"
  1. Update .gitattributes with *.cpp filter=codeformat:
    # All MySQL code is auto-formatted using clang-format.
    *.c filter=codeformat
    *.cc filter=codeformat
    *.cpp filter=codeformat
    *.h filter=codeformat
    *.hpp filter=codeformat
    *.i filter=codeformat
    *.ic filter=codeformat
        
  2. git config
    $ cat .gitconfig
    ...
    [filter "codeformat"]
    <tab>clean = clang-format --assume-filename=%f --style=file
    [merge]
    <tab>renormalize = true
        
  3. MySQL’s .gitconfig says:
    # To automatically add this to your git config so that “git add” and
    # “git merge” automatically run clang-format, use
    #
    #   git config --local include.path ../.gitconfig
        

    But, for submodules, it doesn’t work. Can add the contents of .gitconfig above into:

    $topdir/.git/modules/<module-name>/config

    git config --local filter.codeformat.clean 'clang-format --assume-filename=%f --style=file'
    git config --local filter.codeformat.smudge 'clang-format --assume-filename=%f --style=file'