43cf17fc19273f6c7818ee73510d1de0.gif
先上圖一張,工作中像我這樣使用git的朋友應該不少吧[捂臉]
生成 ssh key
1.ssh-keygen -t rsa -C "youremail@example.com" // 生成ssh key(注意后面填寫自己的郵箱
2.cd ~/.ssh // 進入ssh目錄
3.ls // 查看當前目錄文件 >> id_rsa id_rsa.pub
4.cat id_rsa.pub // 查看公鑰內容(注意是.pub結尾的公鑰文件)
5.將公鑰添加到github,gitlab等的SSH key中
配置
git config --global user.name //獲取當前用戶名
git config --global user.email //獲取當前用戶郵箱
git config --global user.name "your name" //設置用戶名
git config --global user.email "your email" //設置郵箱
初始化
git init // 在文件夾下初始化一個倉庫,此時文件夾里會生成一個.git的隱藏文件夾
配置 git 忽略的文件
不需要 git 管理的文件可寫在根目錄下的 .gitignore 文件中
如:
.DS_Store
.idea/
node_modules/
dist/
npm-debug.log
yarn-error.log
package-lock.json
yarn.lock
加入緩存區(qū)與提交
git add <文件名> // 將該文件加入緩存區(qū)
git add . // 將該所有改變的文件加入緩存區(qū)
git commit -m '說明文字' // 提交一次commit
git push // 推送到遠端
git 分支相關
git clone https://gitee.com/example/example.git // 克隆遠程倉庫到本地
git branch // 查看本地分支
git branch --remote // 查看遠程分支
git branch name // 創(chuàng)建分支
git checkout name // 切換分支
git branch -d name // 刪除分支
git commit -a -m 'some words' // 在當前分支上提交內容
git merge name // 合并name分支到當前分支
git branch -D <分支名> // 刪除本地分支
/* 分支的拉取與推送 */
git pull origin <遠程分支名>:<本地分支名> // 拉取遠程分支的代碼
git push origin <本地分支名>:<遠程分支名> // 推送本地分支到遠程分支
/* 在分支開發(fā)過程中遇到問題需要切換其他分支, 保留寫好的內容再切換分支 */
git stash // 保留內容
git stash pop // 切回之前的分支,回復之前保存的并將其從緩存中清除
git stash apply // 切換回來后需要應用保留的內容/
git stash drop // 丟掉保存的內容
遠程倉庫
git remote add origin <倉庫的地址> // 添加遠程版本庫
查看git狀態(tài)
git status
// On branch 2
// Your branch is up-to-date with 'origin/2'.
// Changes not staged for commit:
// (use "git add <file>..." to update what will be committed)
// (use "git checkout -- <file>..." to discard changes in working directory)
// modified: 1.txt
查看文件改變內容
git diff
// diff --git a/1.txt b/1.txt
// index e69de29..d800886 100644
// --- a/1.txt
// +++ b/1.txt
// @@ -0,0 +1 @@
// +123
版本回退,情況太多,可參參考: https://blog.csdn.net/sinat_29774479/article/details/78599702
1.本地的錯誤提交
git reflog // 查看本地 HEAD
git reset --hard <HEAD> // 回退到本地目標 HEAD
2.自己的遠程分支錯誤提交
git reflog // 查看本地 HEAD
git reset --hard <HEAD> // 回退到本地目標 HEAD
git push -f // 強制推送到遠程分支
3.公共的遠程分支錯誤提交
// 使用以下方式都可撤回
git revert HEAD //撤銷最近一次提交
git revert HEAD~1 //撤銷上上次的提交,注意:數(shù)字從0開始
git revert 0ffaacc //撤銷0ffaacc這次提交
// 之后再 push
git push
附git常用命令一張:
git.jpg