GIT 笔记
获得帮助
$ git help # 获取指定命令的帮助 $ git help <command>
全局设置
$ git config --global user.name "saul" $ git config --global user.email "saul@example.com" $ git config --global color.ui true # 提交消息的生成器. https://github.com/ngerakines/commitment git config --global alias.yolo '!git commit -m "$(curl -s whatthecommit.com/index.txt)"'
Git flow
https://github.com/petervanderdoes/gitflow-avh
The overall flow of Gitflow is
- A develop branch is created from master
- A release branch is created from develop
- Feature branches are created from develop
- When a feature is complete it is merged into the develop branch
- When the release branch is done it is merged into develop and master
- If an issue in master is detected a hotfix branch is created from master
- Once the hotfix is complete it is merged to both develop and master
基本操作
workflow:
+-----------+ +-----------+ +------------+ +------------+ | | | | | Local | | Remote | | Workspace | | Staging | | Repository | | Repository | +-----------+ +-----------+ +------------+ +------------+ | | | | Initialize |<--(1)-------------------------| | |<--(2)-------------------------------------------| | | | | | | | | Update |<--(3)-------------------------------------------| | | |<--(4)-----------| | | | | | | | | Changes |---------(5)-->| | | | |---------(6)-->| | |-------------------------(7)-->| | | | |-----------(8)-->| | | | | | | | | Revert | |<--(9)---------| | |<--(10)--------| | | |<--(11)------------------------| | | | | |
Initialize
1. Local Repository -> Workspace
# 不指定 <directory> 时指当前目录 $ git init [<directory>]
2. Remote Repository -> Workspace
$ git clone <url of remote repository> [<directory>]
Update
3. Remote Repository -> Workspace
$ git pull
4. Remote Repository -> Local Repository
$ git fetch
Changes
5. Workspace -> Staging
$ git add <./dir/file>...
6. Staging -> Local Repository
$ git commit -m <message>
7. Workspace -> Local Repository
# 不会提交 untracked 文件 $ git commit -a -m <message> # 跟上一次的提交合并, 覆盖 message, 同样不会提交 untracked 文件 $ git commit --amend -m <message>
8. Local Repository -> Remote Repository
$ git push [-u <name> <branch>]
Revert
9. Local Repository -> Staging
# 撤销上次提交, 将所有的改动放到 Staging $ git reset --soft HEAD^
10. Staging -> Workspace
# 将文件状态从 Staging 移出, 不指定 <file> 时表示所有在 Staging 的文件 $ git reset HEAD [<file>...] # 丢弃在 Workspace 中的文件改动 $ git checkout -- <file>...
11. Local Repository -> Workspace
# 撤销上次的提交以及所有的改动(慎用) $ git reset --hard HEAD^
其他操作
Diff
# unstaged 与 last commit 比较 $ git diff [<file>] # staged 与 last commit 比较 $ git diff --staged [<file>] $ git diff HEAD $ git diff HEAD^ $ git diff HEAD~5 $ git diff HEAD^..HEAD $ git diff <long_hash>..<long_hash> $ git diff <short_hash>..<short_hash> $ git diff <branch1> <branch2> $ git diff --since=1.week.ago --until=1.minute.ago
Log
$ git log $ git log --pretty=oneline $ git log --oneline $ git log --oneline -p $ git log --oneline --stat $ git log --oneline --graph $ git log --until=1.minute.ago $ git log --since=1.day.ago $ git log --since=1.hour.ago $ git log --since=1.month.ago --until=2.weeks.ago $ git log --since=2000-01-01 --until=2012-12-21
Branch
$ git branch # 列出分支 $ git branch -r # 列出远程分支 $ git branch <branch> # 创建分支 $ git branch -d <branch> # 删除分支 $ git push origin :cat # 删除远程的分支 $ git checkout <branch> # 切换分支 $ git checkout -b <branch> # 创建并切换分支
Tag
$ git tag # 列出标签 $ git checkout <tag> # 切换标签 $ git tag -a <tag> -m <msg> # 新建标签 $ git push --tags # push
Export
$ git archive master | gzip > latest.tgz $ git archive master | bzip2 > latest.tar.bz2