配置環(huán)境變量
git config命令用來配置相應的工作環(huán)境變量,正是這些環(huán)境變量決定了Git在各個環(huán)節(jié)的具體工作方式和行為。這些環(huán)境變量可以存放在三個不同的地方:
- 針對系統(tǒng)所有用戶的$(prefix)/etc/gitconfig,使用時加--system參數。
- 針對當前用戶的~/.gitconfig,使用時加--global參數。
- 針對當前項目的.git/config,使用時加--local參數或不加參數。
使用不同的參數就會讀寫相應地文件,每一個級別都會覆蓋上一級別的相同配置。
// 配置當前用戶下的Git用戶名和郵箱
git config --global user.name name
git config --global user.email email
// 查看當前配置列表
git config --list
// 配置別名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.last 'log -1'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.llg "log -10 --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
版本控制
// 初始化版本庫
git init
// 將文件修改添加到暫存區(qū)
git add
// 提交暫存區(qū)的修改到版本庫
git commit -m ""
// 回退到指定版本
git reset --hard commit_id
// 回退到上一版本
git reset --hard HEAD^
// 撤銷工作區(qū)的修改,回到最近一次提交的狀態(tài)(若暫存區(qū)有提交的修改,則會退到暫存區(qū)修改的狀態(tài))
git checkout -- file_name
// 將提交到暫存區(qū)的修改撤回到工作區(qū)
git reset HEAD file_name
// 刪除一個文件
git rm
// 停止追蹤一個文件[目錄]
git rm [-r] --cached file_name
// 修改最近兩次提交歷史
git rebase [-i|--interactive] HEAD~2
遠程庫相關
// 創(chuàng)建一個遠程庫
git remote add origin git@server-name:path/repo-name.git
// 刪除一個遠程庫
git remote rm origin
//向遠程庫推送最新修改
git push origin branch_name
// 向遠程庫推送最新修改,并建立當前分支與遠程庫對應分支的追蹤關系
git push -u origin branch_name
// 克隆遠程庫
git clone
// 本地創(chuàng)建和遠程分支對應的分支
git checkout -b branch-name origin/branch-name
// 建立本地分支和遠程分支的關聯(lián)
git branch -t[--track] branch-name origin/branch-name
// 建立已有本地分支和遠程分支的關聯(lián)(若不指定branch-name則使用當前分支)
git branch --set-upstream-to=origin/branch-name branch-name
// 清除本地分支與遠程分支的關聯(lián)(若不指定branch-name則使用當前分支)
git branch --unset-upstream branch-name
// 從遠程抓取分支
git pull
// 刪除遠程分支
git push origin --delete branch-name
// 刪除遠程分支后,本地的對應遠程分支依然存在,使用以下命令進行刪除(其中參數-p為prune修剪的意思)
git fetch -p
分支管理
// 查看分支
git branch
// 創(chuàng)建分支
git branch branch_name
// 切換分支
git checkout branch_name
// 創(chuàng)建并切換分支
git checkout -b branch_name
// 合并另一個分支的內容到當前分支
git merge branch_name
// 刪除分支
git branch -d branch_name
// 保存工作現(xiàn)場,清空工作區(qū)
git stash
// 恢復最近一次保存的工作現(xiàn)場,并刪除保存記錄
git stash pop
查看相關信息
// 查看版本庫工作區(qū)狀態(tài)
git status
// 查看文件的修改
git diff
// 查看提交歷史
git log --graph --pretty=onelien --abbrev-commit
// 查看命令歷史
git reflog
// 查看遠程庫信息
git remote -v
標簽
// 查看所有標簽
git tag
// 新建一個標簽(默認為HEAD,也可以指定一個commit_id)
git tag tag_name
// 指定標簽信息
git tag -a tag_name -m ""
// 推送一個本地標簽
git push origin tag_name
// 推送全部未推送過的本地標簽
git push origin --tags
// 刪除一個本地標簽
git tag -d tag_name
// 刪除一個遠程標簽
git push origin :refs/tags/tag_name
暫存
git stash
git stash list
git stash pop
git stash apply stash@{0}
git clear