只梳理在終端常輸入的命令,復雜命令一般用 SourceTree 操作窖式。
本文中的 remote 一般默認就是 origin蚁飒,可相互替代。
創(chuàng)建倉庫
#本地初始化一個倉庫
git init
#克隆遠程倉庫 并重命名倉庫名(可選)
git clone [url] [dir_name]
#克隆遠程倉庫 指定分支
git clone -b <branch> <url> <dir_name>
分支命令
#創(chuàng)建分支
git branch branchName
#切換分支
git checkout branchName
#創(chuàng)建并切換分支
git checkout -b branchName
#創(chuàng)建追蹤遠程的分支
#追蹤分支更新執(zhí)行 git pull ,git push 即可
git checkout -t|[--track] <remote>/<tracked branch> ##創(chuàng)建的分支名跟遠程分支相同
git checkout --track -b <local branch> <remote>/<tracked branch>
git checkout -b branchName <remote>/<tracked branch>
eg: git checkout --track -b dev_20170331 origin/dev_20170331
#跟蹤遠程分支 -u 或 --set-upstream-to
git branch -u <remote>/<branchName>
git push -u <remote>/<branchName>
# 從指定的 創(chuàng)建分支
git checkout tagName -b branchName
#合并分支
git merge branchName
#拉取遠程分支
git pull remote branchName
#推送到遠程分支
git push remote branchName
#更新
git fetch
#刪除本地分支
git branch -d|[-D] branchName
#刪除遠程分支
git push <remote> --delete branchName
提交
#to stage to include/update what will be committed
git add [file]|[dir]|[.]
#to unstage
git rm --cached [-r] <file>
#提交更新
git commit -m msg
#add 并 commit
git commit -a -m msg
git commit -am msg
#替換上次提交記錄 --amend
git commit -m msg --amend
#恢復到工作區(qū) discard changes in working directory
git checkout -- <file>
#恢復 HEAD
git reset --hard HEAD
標簽
#刪除本地標簽
git tag -d tagName
#刪除遠程標簽
git push origin :refs/tags/tagName
配置
#設置大小寫敏感
git config --global core.ignorecase false
#設置顯示中文文件名
git config --global core.quotepath false
#查看配置
git config --list
別名配置
可以簡化命令
可以打開 gitconfig 配置萝喘,然后在 alias 處修改 (推薦)淮逻。
open ~/.gitconfig
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
co = checkout
br = branch
ci = commit
st = status
#查看貢獻
rank = shortlog -sn --no-merges
cob = checkout -b
也可以敲命令配置,作用相同。
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
常用操作
移除遠程的.idea文件夾
有時候一不小心把 .idea 文件夾 push 到了遠程阁簸,這時候再加 gitignore 已經沒用了(已經跟蹤了的文件弦蹂,再無視,是沒有效果的)强窖。
- 執(zhí)行 git rm -fr .idea
- 重新加載項目
- 在 .gitignore 里添加 /.idea 忽視 idea
- 再 gcam “rm idea” git push 到遠程
這個方法也適用于其他的文件。