跳到主要内容

基础

基础

  • 获取帮助信息
    • git help
    • git --help
    • man git
  • git config [--global] user.name "xxx"
  • git config [--global] user.email "[email protected]"
  • git config --list
  • git config user.name
  • git push -u origin master

init

  • git init [指定目录]
  • git init --bare [指定目录]
  • git add .
    • git add -p
  • 跳过暂存区 git commit -a -m 'added new benchmarks'
  • git commit -m '内容'
  • git commit --verbose 会列出 diff 的结果。

clone

git clone -o

保持同步

  • git rebase -i // 压缩历史
    • git rebase -i HEAD~2
    • pick 改为 fixup
  • $ git fetch origin
  • $ git rebase origin/maste

查看

  • git status -v
  • git status -s[hort]

diff

git difftool --tool-help

git difftool

git diff --cached

git diff HEAD

git rm

git rm --cached README

git rm -f

git rm *~

git rm log/*.log

git mv

git mv file_from file_to

分支

  • 合并分支 git merge amazing_new_feature
    • git merge --no-ff amazing_new_feature
  • 查看分支的相关信息 git branch -a
  • 获取分支 git checkout -b feature-D origin/feature-D

合并

remote

log

git reflog // git reflog

git log --oneline --decorate

git log --graph // 以图表形式查看分支

git log -p -2 // revision or path

git log --stat

git log --pretty=oneline

short full fuller

git log --pretty=format:"%h - %an, %ar : %s"

git log --pretty=format 常用的选项

选项说明
%H提交对象(commit)的完整哈希字串
%h提交对象的简短哈希字串
%T树对象(tree)的完整哈希字串
%t树对象的简短哈希字串
%P父对象(parent)的完整哈希字串
%p父对象的简短哈希字串
%an作者(author)的名字
%ae作者的电子邮件地址
%ad作者修订日期(可以用 --date= 选项定制格式)
%ar作者修订日期,按多久以前的方式显示
%cn提交者(committer)的名字
%ce提交者的电子邮件地址
%cd提交日期
%cr提交日期,按多久以前的方式显示
%s提交说明

git log --pretty=format:"%h %s" --graph

git log 的常用选项

选项说明
-p按补丁格式显示每个更新之间的差异
--stat显示每次更新的文件修改统计信息
--shortstat只显示 --stat 中最后的行数修改添加移除统计
--name-only仅在提交信息后显示已修改的文件清单
--name-status显示新增、修改、删除的文件清单
--abbrev-commit仅显示 SHA-1 的前几个字符,而非所有的 40 个字符
--relative-date使用较短的相对时间显示(比如,“2 weeks ago”)
--graph显示 ASCII 图形表示的分支合并历史
--pretty使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)

git log --since=2.weeks

--author

--grep

--all-match

git log -Sfunction_name

限制 git log 输出的选项

选项说明
-(n)仅显示最近的 n 条提交
--since, --after仅显示指定时间之后的提交
--until, --before仅显示指定时间之前的提交
--author仅显示指定作者相关的提交
--committer仅显示指定提交者相关的提交
--grep仅显示含指定关键字的提交
-S仅显示添加或移除了某个关键字的提交

git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \ --before="2008-11-01" --no-merges -- t/

撤销操作

git commit --amend // 修改上一条提交信息

git reset HEAD CONTRIBUTING.md

远程

git remote

git remote -v

git remote add <shortname> <url>

git remote show [remote-name]

git remote rename pb paul

git remote rm

标签

git tag

git tag -l 'v1.8.5*'

附注标签 git tag -a v1.4 -m 'my version 1.4'

轻量标签 git tag v1.4-lw

git tag -a v1.2 9fceb02

git push origin --tags

git push origin v1.5

创建分支并检出 git checkout -b version2 v2.0.0

git reset HEAD -- fileA

查看修改历史

  • git log --pretty=oneline 文件名 # 显示修改历史
  • git show 356f6def9d3fb7f3b9032ff5aa4b9110d4cca87e # 查看更改

恢复

  • git revert HEAD
  • git revert 编号

分支

git log --oneline --decorate --graph --all

git merge master

git branch -v

git branch --merged

git branch --nomerged

还未合并的分支不能删除,可以用-D删除

别名

  • git config --global alias.co checkout
  • git config --global alias.visual '!gitk'

其他

  • git config --global --edit
  • git config --global core.editor vim

权限

用其他账户登陆的话,git pull 时权限会变为其他账户

vim /etc/sudoers

添加 git ALL = (www-data) /usr/bin/git pull

教程