
創(chuàng)建
- 創(chuàng)建本地倉庫:
git init
- 獲取遠(yuǎn)程倉庫:
git clone [url]
例:`git clone https://github.com/you/yourpro.git`
遠(yuǎn)程
- 添加一個(gè)新的 remote 遠(yuǎn)程倉庫:
git remote add [remote-name] [url]
例:git remote add origin https://github.com/you/yourpro.git
留晚,origin:相當(dāng)于該遠(yuǎn)程倉庫的別名
- 列出所有 remote 的別名:
git remote
- 列出所有 remote 的 url:
git remote -v
- 刪除一個(gè) remote:
git remote rm [name]
- 重命名 remote:
git remote rename [old-name] [new-name]
- 修改遠(yuǎn)程倉庫地址:
- 直接修改
git remote set-url origin [url]
例如:`git remote set-url origin https://github.com/you/yourpro.git`
- 先刪后加
git remote rm origin
git remote add origin [url]
提交
- 從本地倉庫中添加新的文件:
git add . // 添加所有文件
git add file.txt // 添加指定文件
- 從本地倉庫中刪除:
git rm file.txt // 從版本庫中移除筹麸,刪除文件
git rm file.txt -cached // 從版本庫中移除粥喜,不刪除原始文件
git rm -r xxx // 從版本庫中刪除指定文件夾
- 提交璧尸,把內(nèi)容提交到 HEAD 里:
git commit -m "注釋"
- 撤銷:
git revert HEAD // 撤銷最近的一個(gè)提交
git reset --mixed // 取消 commit + add
git reset --soft // 取消 commit
git reset --hard // 取消 commit + add + local working
- 把本地提交 push 到遠(yuǎn)程服務(wù)器:
git push [remote-name] [loca-branch]:[remote-branch]
例:git push origin master:master
更新代碼
- 查看狀態(tài):
git status
- 從遠(yuǎn)程庫中下載新的改動-方式1:
git fetch [remote-name]/[branch]
- 從遠(yuǎn)程庫中下載新的改動-方式2:
git pull [remote-name] [branch]
例:`git pull origin master`
pull = fetch + merge
- 合并下載的改動到分支-方式1:
git merge [remote-name]/[branch]
解決沖突...
git add .
git commit -m'fix conflict'
- 合并下載的改動到分支-方式2:
git rebase [remote-name]/[branch]
解決沖突...
git add .
git rebase --continue
分支
// 列出分支
git branch
// 創(chuàng)建一個(gè)新的分支
git branch (branch-name)
// 刪除一個(gè)分支
git branch -d (branch-nam)
// 刪除 remote 的分支
git push (remote-name) :(remote-branch)
// 切換到一個(gè)分支
git checkout [branch-name]
// 創(chuàng)建并切換到該分支
git checkout -b [branch-name]
與github建立ssh通信粥惧,讓Git操作免去輸入密碼的繁瑣
- 首先呢,我們先建立ssh密匙斜脂。
ssh key must begin with 'ssh-ed25519', 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. -- from github
根據(jù)以上文段我們可以知道github所支持的ssh密匙類型藻三,這里我們創(chuàng)建ssh-rsa密匙。
- 在command line 中輸入以下指令:
ssh-keygen -t rsa
去創(chuàng)建一個(gè)ssh-rsa密匙济瓢。如果你并不需要為你的密匙創(chuàng)建密碼和修改名字荠割,那么就一路回車就OK,如果你需要旺矾,請您自行Google翻譯蔑鹦,因?yàn)橹皇怯⑽膯栴}。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Liang Guan Quan/.ssh/id_rsa)
您可以根據(jù)括號中的路徑來判斷你的.ssh文件放在了什么地方
- 到 https://github.com/settings/keys 這個(gè)地址中去添加一個(gè)新的SSH key箕宙,然后把你的xx.pub文件下的內(nèi)容文本都復(fù)制到Key文本域中嚎朽,然后就可以提交了。
- 添加完成之后 我們用
ssh git@github.com
命令來連通一下github扒吁,如果你在response里面看到了你github賬號名火鼻,那么就說明配置成功了。 let's enjoy github ;)
gitignore
在本地倉庫根目錄創(chuàng)建 .gitignore 文件雕崩。Win7 下不能直接創(chuàng)建魁索,可以創(chuàng)建 ".gitignore." 文件,后面的標(biāo)點(diǎn)自動被忽略盼铁;
/.idea // 過濾指定文件夾
/fd/* // 忽略根目錄下的 /fd/ 目錄的全部內(nèi)容
*.iml // 過濾指定的所有文件
!.gitignore // 不忽略該文件