Git一臺電腦多用戶設置方法:
~~~
// 強烈建議使用https,而不使用ssh連接git 遠程repo
git clone https://github.com/user1/repo.git
vim repo/.git/config
// 將[remote "origin"]下的url改為https://user1:pswd@github.com/user1/repo.git
//保存退出
git config user.name "user1"
git config user.email "xxx@email.com"
// 不要使用--global,以保持每個repo擁有特定的身份
// 這樣就可以在一臺電腦上,push多個用戶名下的repo代碼
~~~
查看git倉庫遠程地址:
~~~
git remote -v
~~~
git stash用法:
~~~
//查看隱藏棧列表
git stash list
//清空隱藏棧
git stash clear
//隱藏工作區內容,保持和最近一次提交一致,在merge時經常需要這么做
git stash
//彈出隱藏棧頂層內容,用其恢復工作區
git stash pop
~~~
fork一份repo之后,和源repo保持更新的方法:
~~~
//假設有源repo:source,想要fork它的代碼:
在github頁面上,使用fork功能
//對于fork的repo:mine,將代碼clone到本地:
git clone mine.git
//本地做一些適合自己的修改,并提交到mine repo:
git push origin master
source repo有更新,想要將更新運用到mine repo:
git remote add upstream source.git //只需做一次
git fetch upstream //從source repo中拉取所有更新
git merge upstream/master //將更新合并到origin/master上
git add *; git commit //解決完沖突,需要add & commit以標記沖突解決,消除MERGING狀態
git push origin master //將更新推送到mine repo上
//以上操作都在 origin/master分支上完成
~~~
添加遠程倉庫,以實現git指向多倉庫:
~~~
git remote add upstream https://xxx.github.com/xxx.git
~~~
創建本地分支local,并使其追蹤遠程倉庫upstream的dev分支:
~~~
git checkout -b local upstream/dev
~~~
分支命令
~~~
git branch -r //查看遠程分支列表
git branch -a //查看本地分支和遠程分支
git checkout test //切換到本地的test分支
~~~
將當前工作分支內容推送到遠程倉庫【upstream】的dev分支中:
~~~
git push upstream HEAD:dev
~~~
刪除遠程倉庫【upstream】的dev分支【master分支無法刪除】:
~~~
git push upstream :dev
~~~
丟棄掉本地的修改:
~~~
git checkout <file_path>
~~~