A useful and brief Git cheatsheet.
| 1.Initialization | |
|---|---|
| Init a local repo | git init |
| Associate it with a Github repo | git remote add origin <url> |
| Change the URL | git remote set-url origin <url> |
| Download a remote repo | git clone <url> <path> |
| Check the URL | git remote -v |
| 2.Update | |
| Track all changes | git add . |
| Delete files | git rm <filename> |
| Commit all untracked changes | git commit -am <commit message> |
| Push to remote repo | git push origin <branch name> |
| Update to local repo | git pull |
| 3.Branch Management | |
| Create a branch | git branch <branch name> |
| Switch to branch | git checkout <branch name> |
| Create and switch to a branch | git checkout -b <branch name> |
| Check all branches | git branch |
| Merge a branch back into the master branch | git checkout master && git merge <branch name> |
| Update a branch from remote repo | git pull orgin <branch name> |
| Delete a local branch | git branch -d <branch name> |
| Delete a remote branch | git push origin --delete <branch name> |
| 4.Log and History | |
| See the commit history | git log |
| Show details of a specific commit | git show <commit id> |
| Show changes of a file in a specific commit | git show <commit id> <file name> |
| Checkout form a previous commit | git checkout <commit code> |
| Force pushing | git push -f origin master |
| Combine n local commits | git rebase -i HEAD~<n> |
| Change commit message | git commit -amend |
| Check changed files | git status |
| Compare changed files | git diff --stat |
| 5.Search in the repo | |
| Show the line number of the keyword | git grep -n <keyword> |
| Show the appearence number of the keyword | git grep -c <keyword> |
| 6.Rolling back | |
| Roll back to a commit locally | git reset --hard <commit-id> |
| Roll back to the last (n - 1) commit locally | git reset --hard HEAD~<n> |
| Roll back to a commit remotely (a backup may be necessary.) |
git checkout <branch name> |
| git pull | |
| git reset --hard <commit id> | |
| git push origin --delete <branch name> | |
| git push origin <branch name> | |
Chinese Version:
| 1.新建仓库 | |
|---|---|
| 初始化一个本机仓库 | git init |
| 和Github上的远程仓库url关联 | git remote add origin <url> |
| 更改仓库的url | git remote set-url origin <url> |
| 下载远程仓库到本机 | git clone <url> <path> |
| 查看仓库的url | git remote -v |
| 2.更新及推送 | |
| 跟踪所有已更改文件 | git add . |
| 删除文件 | git rm <filename> |
| 跟踪并提交所有已更改文件 | git commit -am <commit message> |
| 推送至远程仓库 | git push origin <branch name> |
| 更新到本地 | git pull |
| 3.分支管理 | |
| 新建分支 | git branch <branch name> |
| 切换到分支 | git checkout <branch name> |
| 新建并切换到分支 | git checkout -b <branch name> |
| 查看所有分支 | git branch |
| 将分支合并到主分支 | git checkout master && git merge <branch name> |
| 从远程仓库更新分支 | git pull orgin <branch name> |
| 4.日志及历史管理 | |
| 查看所有日志 | git log |
| 回退至某历史版本 | git checkout <commit code> |
| 回退并更改后,强制推送至远程仓库 | git push -f origin master |
| 合并n个本地提交 | git rebase -i HEAD~<n> |
| 更改提交信息 | git commit -amend |
| 查看更改文件的列表 | git status |
| 对比文件更改 | git diff --stat |
| 5.当前库中检索 | |
| 显示关键字的行号 | git grep -n <keyword> |
| 显示在文件的出现次数 | git grep -c <keyword> |