下載安裝客服端sourceTree來管理
下載安裝git
生成秘鑰
安裝好git和sourceTree之后募疮,打開sourceTree裤唠,點擊頂部的“命令行模式”历造,彈出git的窗口操作界面 或者從git Bash進(jìn)入
輸入如下命令以生產(chǎn)gitlab服務(wù)端和本地git相互傳輸時所需要校驗的私鑰和公鑰垃瞧。雙引號內(nèi)輸入的是你git注冊用戶名 郵箱蔫劣∑汗回車后還需要輸入密碼等信息个从,可以不輸入直接回車3次
$ git config --global user.name "你的用戶名"
$ git config --global user.email "你的郵箱"
$ ssh-keygen -t rsa -C "你的郵箱"
默認(rèn)會在/c/Users/Administrator/.ssh目錄中生成秘鑰,如果提示已經(jīng)存在 可重寫,成功后如下顯示:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LpEhcgm0rrnzsEMjo/e1g8n5Xsdw/TXdvSRjihAfkGo 8888888@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .o .. |
| o . .. |
| o + o. . |
| . o E oo .. +|
| . . o.S.. .+ o=|
|o= o.+. o.+.o|
|B... +o o.o. .. |
|++. =..+ . |
|o=...o+. |
+----[SHA256]-----+
配置公鑰
把在/c/Users/Administrator/.ssh目錄中生成的公鑰id_rsa.pub(第二個文件) 粘貼在gitHub 賬戶設(shè)置的SSH KEY中嗦锐,title可以隨便取
配置sourceTree
最后一步就是在sourceTree配置與gitlab公鑰對應(yīng)的私鑰嫌松。點頂部的工具-->選項,配置SSH秘鑰奕污,有個小坑注意下:在SSH客戶端選項中選擇OpenSSH萎羔,中間如果沒有配好的話,在clone工程的時候碳默,會一直提示無效的路徑或沒權(quán)限之類的贾陷。
git 常用命令
查看所有分支git branch -r
查看當(dāng)前分支:git branch
查看當(dāng)前分支狀態(tài):git status
推送分支:git push origin 分支名
切換到主分支:git checkout master
推送分支到主分支:git merge 分支名
查看當(dāng)前總分分支的狀態(tài):git status (可能有沒提交的commit)
推送當(dāng)前的主分支到遠(yuǎn)程:git push origin master origin maste可以省略
找回歷史提交的版本:git reset --hard 提交哈希號
其他基本命令
- 查看當(dāng)前配置
git config --list
- 設(shè)置默認(rèn)提交后顯示的用戶名
git config --global user.name "tony"
git config --global user.email "tony@qq.com"
遠(yuǎn)程倉庫操作
- 查看遠(yuǎn)程倉庫
git remote -v
- 添加遠(yuǎn)程倉庫
git remote add <depot-name> <remote-url>
- 刪除遠(yuǎn)程倉庫
git remote rm <depot-name>
- 修改遠(yuǎn)程倉庫url
git remote set-url --push <depot-name> <new-url>
- 重命名遠(yuǎn)程倉庫
git remote rename <old-depot-name> <new-depot-name>
- 沖遠(yuǎn)程倉庫抓取數(shù)據(jù)
git fetch <depot-name>
分支操作
- 查看遠(yuǎn)程分支
git branch -r
- 查看本地分支
git branch
- 創(chuàng)建分支
git branch <branch-name>
- 修改分支名稱
git branch -m <old-branch-name> <new-branch-name>
- 將分支推送到遠(yuǎn)程服務(wù)器
git push origin <branch-name>
- 切換分支
git checkout <branch-name>
- 刪除本地分支
git branch --delete <branch-name>
- 刪除遠(yuǎn)程分支
git push origin :<branch-name>
- 查看幾次commit的區(qū)別
git diff
- 查看當(dāng)前分支狀態(tài)
git status
- 合并分支到master
git merge <branch-name>
如果有沖突,修改沖突的文件,然后git add ,再git commit
commit操作
- 修改上一次提交的comment
git commit --amend
- 撤銷已git add的文件
git reset HEAD
- 撤銷對文件的修改
git checkout -- <file>
- git撤銷commit
git log查看日志,找到要回退的commit的hash值
git reset --hard <commit_hash_id>
- 回退上一個版本
git reset --hard HEAD^(上一次是HEAD^, 上上次是HEAD^^, 也可寫成HEAD~2,以此類推)
注: --hard 表示放棄所有本地改動
- 清除所有未跟蹤文件,包括納入ignored的文件
git clean -dxf
- 刪除文件,移動文件
git rm <filename>
git mv <old-filename> <new-filename>
出現(xiàn) 以下問題的解決辦法:
- git commit時出現(xiàn) fatal: cannot do a partial commit during a merge.錯誤.
git commit -i <file>
git 分支操作例子
演示創(chuàng)建分支test-branch,修改本地分支嘱根,提交分支遠(yuǎn)程髓废,最后合并分支變更到指定的分支
- 創(chuàng)建分支 test-branch
git checkout -b test-branch
- 修改本地分支,創(chuàng)建一個新的文件 test.txt并提交修改
echo "test" > test.txt
git commit -a -m "add text.txt"
- 提交增加的分支到遠(yuǎn)程
git push origin test-branch
- 合并 test-branch 的內(nèi)容到 master
git checkout master
git merge test-branch
git push
找回歷史提交的版本:git reset --hard 提交哈希號