初始化項目
Git 全局設置:
git config --global user.name "xxxxxx"
git config --global user.email "xxxxxx@163.com"
創(chuàng)建 git 倉庫:
mkdir ocmodel
cd ocmodel
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/Simple_code/ocmodel.git
git push -u origin master
已有倉庫?
cd existing_git_repo
git remote add origin https://gitee.com/Simple_code/ocmodel.git
git push -u origin master
Git 三大分區(qū)
下面介紹一些git里面比較常見的命令行代碼
01-git-help
git help
git help -a//顯示出所有的命令
git help -g//顯示使用手冊
git help add//查看某個命令的幫助
02-git-config
針對用戶設置
git config --global user.name 'name'//全局配置用戶名稱
git config --list
git config --help
git config unset --global user.name//重置name
git config --global user.email 'xxxx@qq.com'//全局配置用戶郵箱
git config --global color.ui true//配置git輸出的顏色
cat ~/.gitconfig//查看git配置
git config user.name 'name'//配置用戶名稱
git config user.email 'xxxx@qq.com'//配置用戶郵箱
03-git-init
mkdir '文件名'
git init//初始化
cd .git//查看.git相關信息
open .git//在mac上使用終端顯示隱藏的.git
04-git-commit
git status
//查看當前狀態(tài)
git add fileName//將Untracked files(工作區(qū)的文件)添加到暫存區(qū)
git add .//添加所有修改的文件
-> changes to commited:
git commit -m '添加描述'//將暫存區(qū)的文件添加到版本庫
git log//查看以往的提交
git add fileName
git commit --amend// 會將修改合并到上一次提交掷匠,不會產生新的提交
05-git-diff
git status
//查看當前狀態(tài)
-> Change not staged for commit
git diff [file_name]//工作區(qū) vs 暫存區(qū)查看修改前后的區(qū)別
(diff=difference)
git diff --staged//repositore和暫存區(qū)的差異
git diff head//工作區(qū) vs 版本庫
git diff --cached//暫存區(qū) vs 版本庫
06-git-rename
filename 修改名稱
git rm 'filename'
git add 'new filename'
git status
07-git-mv
git mv 'old filenmae' 'newfilename'
//修改文件名稱
mkdir 'filename'//新建目錄
git mv 'filename' '新的目錄'//移動文件到新的目錄
08-git-rm
git rm asset/css/o.css
//刪除repository的文件
09-git-head
git rm filename
//刪除repository的文件
git checkout HEAD -- filename//恢復到最近的一次提交
執(zhí)行commit之后的提交
git checkout HEAD^ -- filename//恢復到上一次提交
10-git-revert
git log --oneline
//一行顯示所有提交的日志
git revert id//恢復該id的提交
注意
如果你最近的修改和要撤消的修改有重疊(overlap),那么就會被要求手工解決沖突(conflicts)
git revert HEAD 撤銷前一次 commit
git revert HEAD^ 撤銷前前一次 commit
git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤銷指定的版本,撤銷也會作為一次提交進行保存捶索。
11-git-reset
reset
//指向之前的某一次提交
--soft//repository重置到指定狀態(tài),不會影響到工作區(qū)和暫存區(qū)
--mixed//repository重置到指定狀態(tài),保留工作區(qū)暫存區(qū)重置到到指定的狀態(tài)
--hard//repository重置到指定狀態(tài),工作區(qū)和暫存區(qū)直接重置到指定的狀態(tài)
git log --online//查看提交日志
git reset --soft id
git reset --mixed id
git reset --hard id
使用git push -f
提交更改:
此時如果用git push
會報錯,因為我們本地庫HEAD指向的版本比遠程庫的要舊`
git reset --hard origin/master
拋棄我所有的分階段和未分階段的更改,忘記當前的本地分支上的一切裕偿,使它與origin / master完全相同引润。
12-git-branch
git status
//查看分支
*master//*表示在當前分支
git branch branchName//創(chuàng)建分支
git branch branchName commitid//根據(jù)commitid創(chuàng)建分支
git checkout commitid -b branchName//本地新拉出分支的名稱
git brance//查看項目分支
13-git-checkout
git checkout 分支name
//切換分支
git log --oneline --decorate//每行日志顯示分支名稱
14-git-branch-diff
git diff 分支名稱1..分支名稱2
//對比兩個分支的差異
f//向下翻頁
git diff 分支名稱1..分支名稱2 filename//某個分支對比兩個分支的差異
15-git-fast-forward
子分支未進行add. commit之后
16-git-merge
主分支和子分支進行commit之后
git log --oneline --decorate --all -10 --graph//查看所有分支的10信息
git merge 子分支//將子分支commit的文件合并當前分支
17-git-conflict
//在文件里面找到沖突代碼
A合并到B
-<<<<<<<<< HEAD
B的代碼
-=============
A的代碼
->>>>>>>>>>>>>
18-git-rm-branch
git branch -m 分支名 新的分支的名字
//修改分支名字
git branch -d 分支名//刪除分支
git branch//查看本地分支
git branch -a//查看本地遠程分支
git branch -r//查看遠程分支
git checkout 分支名 ``
git push origin --delete [branch_name] //刪除遠程分支
19-git-stash
git stash save '描述信息'
//暫時保存工作區(qū)的內容到暫存區(qū)
git stash list//顯示緩存的stash列表
git stash show//查看緩存的stash,默認第一個
git stash show -p stash@{0}//查看緩存的stash@{0}
git stash apply stash@{0}恢復緩存的stash@{0}
git stash pop恢復工作區(qū)的內容,刪除緩存的stash,默認第一個
git stash pop stash@{0}恢復工作區(qū)的內容栈幸,刪除緩存的stash@{0}
git stash drop stash@{0}刪除緩存的stash@{0}
git stash clear 刪除所有緩存的stash
20-git-log
git log
f向下 b向上
git log --online -5//指定顯示行數(shù)
git log --online --author="name"//指定作者
git log --online --grep='index.html'//指定包含index.html
git log --online --before='2014-07-05'//指定某個時間之前的提交
git log --online --before='3 days'//指定3天之前的提交
git log --online --before='1 week'//指定1周之前的提交
git log --online --graph//加上圖像效果
git reflog 和 git log 區(qū)別
1、git log 是線上和本地的提交記錄
2帮辟、git reflog(reference log參考日志的縮寫)是本地的操作記錄速址,除了提交還有刪除/merge等記錄,一般用于獲取回滾的commitid由驹。
21-git-alias
git config --global alias.co checkout
//checkout命令添加別名co
cat ~/.gitconfig
vim ~/.bashprofile//通過系統(tǒng)添加別名
22-git-ignore
git config --global core.excludesfile ~/.gitignore_global
//添加忽略文件
vim ~/. gitignore_global//編輯忽略文件
.DS_Store
23-git-gitgnore
為項目添加忽略文件
vim gitignore
24-git-remote
git remote
//遠程版本庫
25-git-origin
git remote add origin xxx地址
//將本地的git提交至遠程
26-git-push
git push -u origin master
//提交master分支
git branch -a//查看本地分支
git branch -r//查看遠程分支
27-git-remove-workflow
fork
//繼續(xù)開發(fā)
28-git-clone
git clone xxx
//將代碼從遠程庫克隆到本地
git clone xxx 文件夾名//將代碼從遠程庫克隆到本地并創(chuàng)建相應的文件夾
29-git-fetch
git fetch
//拉去代碼
git merge orgin/master//合并分支
30-git-fork
fork
基于項目繼續(xù)開發(fā)
31-git-pull-request
pull-request
//提交文件修改到主項目
32-git-collaborator
項目協(xié)作者
33-git-github-tools
github-tools git圖像化工具
34- 解決revert誤操作
git reflog
//查看本地commit記錄,找到對應的commit id
git reset --hard [commit id]//回退到某個版本
35-git-tag
git tag <tagName>
//創(chuàng)建本地tag
git push origin <tagName>//推送到遠程倉庫
git push origin --tags//若存在很多未推送的本地標簽壳繁,一次全部推送
git show <tagName>//查看本地某個 tag 的詳細信息
git tag 或者 git tag -l//查看本地所有 tag
git ls-remote --tags origin//查看遠程所有 tag
git tag -d <tagName>//本地 tag 的刪除
git push origin :<tagName>//遠程 tag 的刪除
git checkout -b <branchName> <tagName>//檢出標簽
git tag -a <tagname> -m "XXX..."//可以指定標簽信息
git tag -a v0.1.0 -m "release 0.1.0 version"//創(chuàng)建附注標簽
git checkout [tagname]//切換標簽
·
36-修改 .gitignore 文件生效
#清除緩存
git rm -r --cached .
#重新trace file
git add .
#提交和注釋
git commit -m "update .gitignore"
#可選,如果需要同步到remote上的話
git push origin master
37-遠程分支刪除荔棉、本地分支更新
git remote update origin -p
36-git-cherry-pick-將某一次提交合并到另一個分支
# 查找想要同步的提交的哈希值
git log # 找到你想要的提交的哈希值
# 假設提交哈希值是abc1234闹炉,目標分支是feature-branch
git checkout feature-branch # 切換到目標分支
git cherry-pick abc1234 # 應用該提交的變化