Git 經常使用命令備份

繼上次保存了git 多個key共存配置(http://www.javashuo.com/article/p-fwdzmhzy-p.html)後,今天把常見的git命令備份下(最近個人雲筆記帳號常常出問題)html

 

 

下面是解決基本的衝突辦法git

遇到衝突要冷靜,若是不肯定怎麼操做是對的,就請教周圍的高手,退一萬步來講,先把本身的本地代碼打包備份再說下一步.....github

 

曾經一個年少 」無知」 的剛畢業的同事,pull代碼衝突了,本身不知道怎麼操做了,就瞎搞一通,後來搞不定了求助於我,我一看,怎麼遠程分支都被刪了!!!   暈!bash

 

請教別人各種問題並沒啥,臉皮厚 吃個夠  O(∩_∩)O哈哈~ssh

 

 

##############################GIT 使用#########################
#1.generate ssh-key and config on gitlab
ssh-keygen -t rsa -f ljkj028Key -C "myLjkj028Key"gitlab

#-f :use custom fileName default name is [id_rsa]
#-C :to remark this key in order to distinguish to other key
#such as: ssh-keygen -t rsa -C "ljkj028Key"測試

# and if you used -f and not the default file name,you should do more work following:
#a:open ssh-agent through [ssh-agent -s] if you use github bash or use [eval $(ssh-agent -s)] if you use other bash
#b:add private key through [ssh-add ~/.ssh/ljkj028Key]
#then you can use git to clone code from the gitlab which you public key configuredfetch

#if you has more than one pulic key for different gitlab, you should config it at file named config
Host git.ljkj028.com
HostName git.ljkj028.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/ljkj028Key
User ljkj028ui

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/githubKey
User xiaochangweithis

#notice: you should do [ssh-add ~/.ssh/ljkj028Key] for each key


#2.全局信息設置
git config --global user.name "xiaochangwei"
git config --global user.email "changw.xiao@qq.com"

#項目克隆與提交
#3.1克隆項目到新目錄修改並提交
git clone git@gitee.com:xiaochangwei/testCode.git
cd testCode
touch README2.md
git add README2.md
git commit -m "add README2"
git push -u origin master

#3.2.將本地未和任何倉庫關聯的代碼推送到遠程倉庫
cd myLocalDir
git init
git remote add origin git@gitee.com:xiaochangwei/testCode.git
git pull git@gitee.com:xiaochangwei/testCode.git master
touch t.txt
git add .
git commit -m "Initial commit"
git push -u origin master

#3.3.將本地已經和其它倉庫關聯的代碼推送到新倉庫
cd myLocalDir
git remote rename origin old-origin
git remote add origin git@gitee.com:xiaochangwei/ci-demo.git
git pull git@gitee.com:xiaochangwei/ci-demo.git master
#注意是否有衝突
git push -u origin --all
git push -u origin --tags

#########normal develop##########
git checkout develop
#each developer checkout one or more feature to develop
git checkout -b sprintXfeatureX
#sprintXfeatureX related develop complete and passed developer self test
git add .
git commit -am "sprintXfeatureX completed"
#git push -u origin sprintXfeatureX (能夠只在本地保留分支信息,不推送到遠端)
#merge to develop and tell testers that they can start work
git checkout develop
git pull
git merge sprintXfeatureX
git push

#after this sprint onlined, you can delete functional branch. However, it is suggested that branches be retained more than half a year.
#git push origin :sprintXfeatureX
#git branch -d sprintXfeatureX


#testers start work
#testers shoud clone a new branch from develop at the each sprint start
git checkout dev
git checkout -b releaseSpringX
#if has cloned the releaseSpringX
git checkout releaseSpringX
git merge develop
git push

#if there is some bug, developer shoud checkout a new branch from releaseSpringX
git checkout releaseSpringX
git checkout -b releaseSpringXBug01
#do something
git add .
git comit -am "Bug01 fixed";
#git push -u origin releaseSpringXBug01(能夠只在本地保留分支信息,不推送到遠端)
#notice tester merge code to releaseSpringX bug
git checkout releaseSpringX
git pull
git merge releaseSpringXBug01
git push

#after passed test, tester merge releaseSpringX to master && develop and give a tag
git checkout master
git merge releaseSpringX
git push
git tag releaseSpringX OR git tag -a releaseSpringX -m 'SpringX external release' OR git tag -a releaseSpringX CommitID
git push origin releaseSpringX OR git push origin --tags

git checkout develop
git merge releaseSpringX
git push

#search and view tag detail
git tag OR git tag -l 'releaseSpringX*'
git show releaseSpringX

#and also you can delete local and remote tag same as branch operation
git tag -d releaseSpringX
git push origin :releaseSpringX


#########when online bug need fix##########
git checkout master
git checkout -b hotfix01
#do sth
git add .
git commit -am "hotfix01 fixed"
#git push -u origin hotfix01 (能夠只在本地保留分支信息,不推送到遠端)
#test passed at hotfix01
git checkout master
git pull
git merge hotfix01
#merge hotfix01 code to branch develop
git checkout develop
git pull
git merge hotfix01
git push

#delete branch hotfix01(local&remote)
git branch -d hotfix01
git push origin :hotfix01 or use: git push origin --delete hotfix01


#when the tester encountered conflict at the operation of pull or merge, it must be confirm with developer which one is correct

#查看最近n次提交日誌且以單行顯示
git log -n --oneline


#######撤銷與恢復某些文件#############
#conflict and reset
#若是你只是修改了文件並且尚未用git add將修改加入提交,想恢復到修改以前
git checkout -- t.txt

#能夠模糊匹配
git checkout -- '*.txt'

#若是已經經過 git add t.txt 將修改了的文件加入了提交
git reset HEAD -- t.txt

#若是要恢復到某個已經commit的版本
git reset commitID -- fileName
#這時候commitID對應的文件已經放到了暫存區,須要checkout到工做區
git checkout fileName
#修改後add commit push
#checkout後,以前工做區的內容將被覆蓋,工做區和暫存區的內容均爲commitID對應的

##########恢復整個分支到某一歷史版本#################
#經過git log判斷某個commitID是屬於哪個分支
git branch -r --contains 4560a9f


#恢復以前必定要備份當前分支並肯定CommidID屬於當前分支,不然會恢復到其它分支,
#!!!恢復後歷史版本後的提交記錄都將不存在,【但以前打的tag是存在的】
git reset --hard d7ed38b
git push -f -u origin master

##########拉取某個tag###########
#能夠經過git checkout tagName 獲取指定tagName處的代碼,獲取以後分支號顯示爲:((tagName))
#但當前指針處在分離頭指針狀態,這個時候的修改是很危險的,在切換回主線時,以前的修改提交基本都會丟失
#在add commit以後 分支號顯示爲:((CommitID))
#這時候是沒法push的,由於未和任何分支關聯
#git也會提示根據commitID建立分支來進行操做 git branch <new-branch-name> CommitID

#因此通常都基於tagName來新建一個分支,修改完畢後合併到某個分支,但合併時須要注意別讓舊代碼覆蓋了新代碼
git checkout -b newBranch tagName


#######同步全部信息###########
git fetch origin

#####查看remote 地址######
git remote -v

 


git@gitee.com:xiaochangwei/merge.git

git@gitee.com:xiaochangwei/rebase.git


#############分支合併###############
1.從develop拉取開發分支
git checkout develop
git checkout -b feature01

2.開發....

3.git add .
4.git commit -am "remark"

#獲取develop最新代碼,並本地合併測試
git checkout develop
git pull

git checkout feature01
git rebase -i HEAD~2 ---->合併提交 --- 2表示合併兩個
#將本地的屢次提交合併爲一個,以簡化提交歷史。本地有多個提交時,若是不進行這一步,在git rebase develop時會屢次解決衝突(最壞狀況下,每個提交都會相應解決一個衝突)
git rebase develop ---->解決衝突--->git rebase --continue

#合併develop的最新代碼後自測經過

#合併代碼到develop
git checkout develop
git merge feature01
git push

#若是有衝突且沒使用git rebase -i,則衝突是一個一個報出來,不像merge一次性所有報出來
#因此解決了一個衝突後須要執行下列命令,繼續rebase ,故推薦使用git rebase -i
git add -u
git rebase --continue

#終止rebase,恢復到rebase前的狀態git rebase --abort

相關文章
相關標籤/搜索