Git 常用命令備份


繼上次保存了git 多個key共存配置(https://www.cnblogs.com/xiaochangwei/p/9155195.html)后,今天把常見的git命令備份下(最近我的雲筆記賬號經常出問題)

 

 

下面是解決基本的沖突辦法

遇到沖突要冷靜,如果不確定怎么操作是對的,就請教周圍的高手,退一萬步來說,先把自己的本地代碼打包備份再說下一步.....

 

曾經一個年少 ”無知” 的剛畢業的同事,pull代碼沖突了,自己不知道怎么操作了,就瞎搞一通,后來搞不定了求助於我,我一看,怎么遠程分支都被刪了!!!   暈!

 

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

 

 

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

#-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 configured

#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 ljkj028

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

#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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM