涓€瀛﹀氨浼氱殑git鍛戒护
Posted FishStudy520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓€瀛﹀氨浼氱殑git鍛戒护相关的知识,希望对你有一定的参考价值。
涓€瀛﹀氨浼氱殑git 鍛戒护
瀵艰
浣跨敤git 宸蹭笉鐭ュソ鍑犲勾锛屾渶杩戞兂鐫€鎶婂父鐢ㄧ殑涓€浜沢it 鍛戒护鍋氫釜鎬荤粨锛屾湰鏂囦富瑕佸啓鐨勬槸git甯哥敤鐨勫懡浠ゃ€?/p>
闃呰涓夎繛锛氱偣璧烇紙馃憤锛夈€佸叧娉紙馃槏锛夈€佹敹钘忥紙馃摑锛夈€?/strong>
鏈枃 github (涓€瀛﹀氨浼氱殑Git鍛戒护鎿嶄綔) 宸蹭笂浼狅紝鏇村寰€鏈熸枃绔犲凡鍒嗙被鏁寸悊銆?/strong>
鍑犱釜甯哥敤鐨勫懡浠?/h4>
mkdir folder_name // 鍒涘缓鏂囦欢澶?[folder_name: 鏂囦欢澶瑰悕绉?涓句釜鏍楀瓙锛歡it-demo]
cd folder_name // 杩涘叆鏂囦欢澶?touch file_name // 鍒涘缓涓€涓枃浠?[file_name:鏂囦欢鍚嶇О锛屼妇涓牀瀛愶細index.js]
vim file_name // 缂栬緫涓€涓枃浠?[涓句釜鏍楀瓙锛歷im index.js]
rm -rf file_name // 鍒犻櫎鏂囦欢鎴栬€呮枃浠跺す [rm -rf file_name]
ls // 鏄剧ず宸ヤ綔鐩綍
clear // 娓呴櫎灞忓箷
鑾峰彇git 浠撳簱
mkdir folder_name // 鍒涘缓鏂囦欢澶?[folder_name: 鏂囦欢澶瑰悕绉?涓句釜鏍楀瓙锛歡it-demo]
cd folder_name // 杩涘叆鏂囦欢澶?touch file_name // 鍒涘缓涓€涓枃浠?[file_name:鏂囦欢鍚嶇О锛屼妇涓牀瀛愶細index.js]
vim file_name // 缂栬緫涓€涓枃浠?[涓句釜鏍楀瓙锛歷im index.js]
rm -rf file_name // 鍒犻櫎鏂囦欢鎴栬€呮枃浠跺す [rm -rf file_name]
ls // 鏄剧ず宸ヤ綔鐩綍
clear // 娓呴櫎灞忓箷
1.绗竴绉嶆柟寮忥紝灏嗘湰鍦伴」鐩浆鎹㈡垚git 浠撳簱, 鐢熸垚涓€涓?git鏂囦欢锛?/p>
mkdir git-demo //鍒涘缓涓€涓枃浠?cd git-demo
git init // 鍒涘缓涓€涓悕涓?.git 鐨勫瓙鐩綍
2.绗簩绉嶆柟寮忥紝鎷夊彇(鍏嬮殕)杩滅▼宸插瓨鍦ㄧ殑git浠撳簱锛?/p>
git clone <url> // url 浠撳簱鍦板潃
git clone https://github.com/FishStudy520/git-demo.git
git config (閰嶇疆鐢ㄦ埛淇℃伅)
閰嶇疆鐢ㄦ埛淇℃伅锛堢敤鎴峰悕鍜岄偖绠憋級锛?/p>
git config user.name \'your name\' // 閰嶇疆鐢ㄦ埛鍚?git config user.email \'your email\' //閰嶇疆鐢ㄦ埛閭
鏌ョ湅閰嶇疆淇℃伅 (鏄剧ず閰嶇疆淇℃伅)锛?/p>
git config --list
鍏ㄥ眬閰嶇疆鐢ㄦ埛鍙渶瑕佸姞 --global
锛?/p>
git config --global user.name \'your name\' // 鍏ㄥ眬閰嶇疆鐢ㄦ埛鍚?/code>
git help (鏌ョ湅鏂囨。)
鏌ョ湅甯歌鐨勫懡浠わ紱
git help
鏌ョ湅git鍏ㄩ儴鐨勫懡浠わ紱
git help -a
鏌ョ湅git鍛戒护鏌愪釜鏂囨。, 涓句釜鏍楀瓙锛?/p>
git help add
git status (鏌ョ湅鐘舵€?
鏌ョ湅鐘舵€?/p>
git status
涓昏鐢ㄤ簬鏌ョ湅宸ヤ綔鍖轰笌淇濆瓨鍖虹殑鐘舵€侊紱鏂囦欢鐘舵€佸浠ヤ笅涓夌锛?
Untracked files: 鏈窡韪殑鏂囦欢锛屾柊鍒涘缓鐨勬枃浠?锛堟湭璺熻釜鐨勬枃浠讹級;
Changes to be committed:淇濆瓨宸蹭慨鏀圭殑鏂囦欢锛岃鏂囦欢杩愯鍦╣it add 鐨勭増鏈巻鍙茶褰曚腑锛屼絾杩樻病鏈塩ommit ;
Changes not staged for commit: 淇濆瓨宸蹭慨鏀圭殑鏂囦欢;
- 鏈窡韪殑鏂囦欢(Untracked)鍜屽凡淇敼鐨勬枃浠讹紙Modified锛夐€氳繃
git add .
鎻愪氦鍒版殏瀛樺尯鍖猴紝浣跨敤git reset HEAD <file>
杩樺師鍒?code>git add .鏃剁殑鐘舵€侊紱 - 宸蹭慨鏀圭殑鏂囦欢锛圡odified锛変娇鐢?code>git checkout .灏嗕互璺熻釜鎵€鏈夊凡淇敼鐨勬枃浠讹紝杩樺師鍒颁慨鏀瑰墠鐨勭姸鎬侊紙Unmodifed锛夛紱
- 浠ヨ窡韪湭淇敼鐨勬枃浠讹紙Unodified锛夐€氳繃鏈湴浠撳簱鍒囨崲鍑烘柊鍒嗘敮鎵€寰楋紱
鏌ョ湅鐘舵€侊紝绠€鍐欏弬鏁帮細-s
git status -s
git add锛堟坊鍔犲埌鏆傚瓨鍖猴級
灏嗘枃浠?鏂囦欢澶规彁浜ゅ埌鏆傚瓨鍖猴紝娣诲姞鍗曚釜鎴栬€呭涓?
git add file file1 ...
灏嗗綋鍓嶇洰褰曚笅鐨勬墍鏈夋枃浠舵坊鍔犲埌鏆傚瓨鍖猴紙鍖呭惈淇敼鐨勬枃浠朵互鍙婃柊澧炴枃浠讹級锛?/p>
git add .
灏嗗凡缁忕洃鎺х殑鏂囦欢娣诲姞鍒版殏瀛樺尯锛堜笉鍖呭惈鏂板鐨勬枃浠讹級锛?/p>
git add -u
灏嗗綋鍓嶉」鐩笅鐨勬墍鏈夊彉鍖栭兘娣诲姞鍒版殏瀛樺尯锛?/p>
git add -A
git commit锛堟彁浜ゆ湰鍦颁粨搴擄級
Git commit灏嗗伐浣滃尯鐨勫唴瀹规彁浜ゅ埌鏈湴浠撳簱锛屾瘡娆℃彁浜ょ敓鎴愪竴涓猚ommit-id锛屽湪鐗堟湰鍥為€€璧蜂簡閲嶈鐨勪綅缃€?/p>
灏嗗伐浣滃尯鍐呭鎻愪氦鍒版湰鍦颁粨搴?
git commit -m \'message\'
灏嗘殏瀛樺尯宸蹭慨鏀圭殑鏂囦欢鎻愪氦鍒版湰鍦颁粨搴?娉細宸茬増鏈帶鍒剁殑鏂囦欢锛?/p>
git commit -a -m \'messgae\'
濡傛灉鎴戜滑鏈€杩戜竴娆$殑鎻愪氦淇℃伅鍐欓敊浜嗭紝鍙互浣跨敤涓嬮潰鍛戒护淇敼锛?/p>
git commit --amend
git 鍒嗘敮
鏌ョ湅鏈湴浠撳簱涓嬬殑鍒嗘敮,鏍囪鈥?鈥濈殑鏄綋鍓嶅伐浣滃尯鍒嗘敮锛?/p>
git branch
鏌ョ湅鎵€鏈夌殑鍒嗘敮锛堟湰鍦颁粨搴撲互鍙婅繙绋嬩粨搴撳垎鏀級锛?code>-a 鏄?--all
鐨勭畝鍐欙紱
git branch -a
鍒涘缓涓€涓柊鍒嗘敮锛?/p>
git branch <branch_name> //branch_name: 鏂板垎鏀悕绉?/code>
鍒囨崲鍒嗘敮,姣斿浣犵幇鍦ㄥ湪master鍒嗘敮涓婏紝鍒囨崲鍒癲evelop鍒嗘敮锛?/p>
git checkout develop // 鍒囨崲鍒癲evelop鍒嗘敮
鍒犻櫎鏌愪釜鍒嗘敮锛?code>-d 鏄?--delete
鐨勭畝鍐欙紱
git branch -d <branch_name> // branch_name锛氶渶瑕佸垹闄ょ殑鍒嗘敮鍚?// 涓句釜鏍楀瓙锛氬垹闄evelop 鍒嗘敮
git branch -d develop
// 寮哄埗鍒犻櫎鏌愪釜鍒嗘敮锛屽垎鏀湭鍚堝苟锛屽鏋滄兂鍒犻櫎锛屽氨浣跨敤涓嬮潰鍛戒护锛?git branch -D develop
鎵撳嵃褰撳墠鍒嗘敮鍚嶇О锛?/p>
git branch --show-current
鏄剧ず涓庡伐浣滃尯鍒嗘敮宸插悎骞剁殑鍒嗘敮锛?/p>
git branch --merged
鏄剧ず涓庡伐浣滃尯鍒嗘敮鏈悎骞剁殑鍒嗘敮锛?/p>
git branch --no-merged
git checkout (妫€鍑?
git checkout
涓昏鐨勪綔鐢ㄦ槸鍒囨崲鍒嗘敮鎴栬€呰繕鍘熷伐浣滃尯鏂囦欢鐨勫彉鏇淬€?/p>
鍒囨崲鍒嗘敮锛?/p>
git checkout <branch_name> // branch_name:鍒嗘敮鍚嶇О
git checkout develop //鍒囨崲鍒癲evelop 鍒嗘敮
鍒涘缓鏂板垎鏀苟涓斿垏鎹㈠埌杩欎釜鍒嗘敮涓婏紱
git checkout -b <branch_name> // branch_name:鏂板鍒嗘敮鍚嶇О
git checkout -b develop // 鍒涘缓develop 鍒嗘敮骞跺垏鎹㈠埌develop鍒嗘敮涓?/code>
蹇界暐宸ヤ綔鍖虹殑淇敼锛屽垏鎹㈠垎鏀紱
git checkout -f <branch_name> // 蹇界暐淇敼鍒囨崲鍒?branch_name 涓?/code>
git merge(鍚堝苟)
git merge
鐨勪綔鐢ㄥ氨鏄皢鎸囧畾鐨勫垎鏀悎骞跺埌褰撳墠鍒嗘敮锛?/p>
鍚堝苟鍒嗘敮锛?/p>
git merge <branch_name> // branch_name: 鍒嗘敮鍚嶇О
鍦ㄥ悎骞跺垎鏀椂锛屼骇鐢熷悎骞跺啿绐侊紝閫€鍥炲埌鍚堝苟鍓嶇殑鐘舵€侊紱
git merge --abort
git reset
git reset
鐢ㄤ簬鍥為€€鐗堟湰锛屽彲浠ユ寚瀹氶€€鍥炴煇涓€娆℃彁浜ょ殑鐗堟湰銆?/p>
鍥為€€鍐呭鍒版煇涓増鏈紱
git reset HEAD^ // 灏嗘墍鏈夋枃浠惰繕鍘熶笂涓€涓増鏈?git reset HEAD^ <file_name> // 灏嗘煇涓枃浠惰繕鍘熶笂涓€涓増鏈?git reset <commit-id> // 灏嗘枃浠惰繕鍘熷埌鏌愪釜鐗堟湰
git log(鎻愪氦璁板綍)
鏌ョ湅鎵€鏈夌殑鎻愪氦璁板綍锛?/p>
git log // 鎸?瀛楁瘝 q 閫€鍑烘煡鐪?/code>
鍙傛暟
--oneline: git log 鐨勭畝鍐欙紝杈撳嚭7涓猚ommit hash 鐨勫墠7涓瓧绗︼紝浠ュ強鎻愪氦璇存槑锛?/p>
git log --oneline
-p锛氭煡鐪嬫瘡娆℃彁浜ゆ敼鍙樼殑宸紓锛岃繖涓緢璇︾粏锛?/p>
git log -p // 鏌ョ湅姣忔鎻愪氦鏀瑰彉鐨勫樊寮?git log -p <file> // 鏌ョ湅鎸囧畾鏂囦欢鐨勬彁浜よ褰?/code>
--stat锛氭煡鐪嬫瘡娆℃彁浜ょ殑绠€鐣ヤ俊鎭紝鍙互蹇€熸煡鐪嬫瘡娆℃彁浜ょ殑宸紓锛屾瘮-p绠€娲侊紱
git log --stat
--pretty锛氳繖涓弬鏁扮敤浜庤嚜瀹氫箟杈撳嚭鏍煎紡淇℃伅锛涙瘮濡傦細oneline[鍗曡],format[瀹氬埗鏃堕棿鏍煎紡]
git log --pretty=oneline
-n: n浠h〃闄愬埗杈撳嚭鐨勬暟閲忥紝鏈€杩戠殑n鏉℃彁浜や俊鎭紱
git log -2 // 鏈€杩戜袱鏉$殑鎻愪氦淇℃伅
--graph锛氭煡鐪嬪湪鏃ュ織鏃佷互 ASCII 鍥惧舰鏄剧ず鍒嗘敮涓庡悎骞跺巻鍙诧紱
git log --graph
git diff(鏌ョ湅宸紓)
鏌ョ湅褰撳墠宸ヤ綔鍖轰笌淇濆瓨鍖烘枃浠朵慨鏀逛簡鍝簺鍐呭锛堜笉鍖呭惈鏂版枃浠讹級锛?/p>
git diff
鏌ョ湅褰撳墠宸ヤ綔鍖轰笌涓婁竴涓増鏈殑宸紓锛?/p>
git diff HEAD
git diff commit-id // 涓巆ommit-id鐗堟湰姣旇緝宸紓锛宑ommit-id锛氬搴旀彁浜ょ殑鐗堟湰鍙?/code>
鏌ョ湅褰撳墠宸ヤ綔鍖轰笌鍙︿竴涓垎鏀殑宸紓锛?/p>
git diff master // 涓巑aster 鍒嗘敮姣旇緝宸紓
鏌ョ湅瀵规瘮涓や釜鍒嗘敮闂寸殑宸紓锛?/p>
git diff master...develop // 瀵规瘮master鍒嗘敮涓巇evelop 鍒嗘敮鐨勫樊寮?/code>
鏌ョ湅鍝簺鏂囦欢鏀瑰姩浜嗭紝鏈夊灏戯紝鍙互浣跨敤--stat
鍙傛暟锛?/p>
git diff --stat
git tag(鎵撴爣绛?
git tag
鐢ㄦ潵鏍囪git 鐗堟湰鐨勬爣璁帮紱
鏌ョ湅鎵€鏈夌殑tag 鏍囪锛?/p>
git tag
鍒涘缓涓€涓猼ag鏍囪;
git tag v0.0.1
鏌ョ湅鏌愪釜tag鐨勮鎯呭唴瀹癸紱
git tag <tag_name> // tag_name: tag鍚嶇О
// 姣斿锛氭煡鐪媣0.0.1 鏍囪
git tag v0.0.1
鍒犻櫎鏌愪釜tag;
git tag -d v0.0.1
鎺ㄩ€佹湰鍦皌ag鍒拌繙浠撳簱锛?/p>
git push origin --tags
鏌ョ湅杩滅▼浠撳簱鐨勬墍鏈塼ag;
git ls-remote --tags origin
浠ユ煇涓猼ag 鍒涘缓鏂板垎鏀紱
git checkout -b <branch_name> <tag_name> // branch_name:鏂板垎鏀悕锛宼ag_name锛?tag鍚?/code>
git 杩滅浠撳簱
鏌ョ湅杩滅▼浠撳簱锛?/p>
git remote -v
娣诲姞杩滅▼浠撳簱锛?/p>
git remote add <short_name> <url> // short_name: 绠€鐭殑鍚嶇О锛寀rl锛氳繙绋嬩粨搴撳湴鍧€
// 涓句釜鏍楀瓙锛?git remote add fishStudy520 https://github.com/FishStudy520/git-demo.git
浠庤繙绋嬩粨搴撴媺鍙栧拰鎶撳彇鍒版湰鍦颁粨搴擄紝浣嗘湭鍚堝苟锛堝悓姝ヨ繙绋嬩粨搴撴暟鎹級锛?/p>
git fetch <remote> //remote锛?杩滅▼鍦板潃
浠庤繙绋嬩粨搴撴媺鍙栧拰鎶撳彇鍒版湰鍦颁粨搴擄紝骞朵笖鍚堝苟鏁版嵁锛堝悓姝ヨ繙绋嬩粨搴撴暟鎹級锛?/p>
git pull origin master // 鎷夊幓杩滅▼浠撳簱鏁版嵁
鍒犻櫎杩滅▼浠撳簱鐨勬煇涓垎鏀紱
git push origin --delete <branch_name> // branch_name: 杩滅鍒嗘敮鍚?/code>
鎺ㄩ€佸埌杩滅▼浠撳簱锛?/p>
git push origin master // 鎺ㄩ€佸埌杩滅▼浠撳簱鐨刴aster鍒嗘敮
鏌ョ湅鏌愪釜杩滅▼浠撳簱锛?/p>
git remote show origin
淇敼杩滅▼浠撳簱鍚嶇О锛?/p>
git remote rename old_name new_name
// 涓句釜鏍楀瓙锛? git remote rename fishStudy520 fish001
绉婚櫎杩滅▼浠撳簱锛?/p>
git remote remove fishStudy520 //绉婚櫎杩滅▼浠撳簱 fishStudy520
git pull 涓?git fetch 鐨勫尯鍒細
git pull = git fetch + git merge
璐棌涓庢竻鐞?/h4>
鎵ц璐棌鎿嶄綔锛屾槸灏嗕慨鏀规殏鏃跺瓨鍌ㄥ埌鍫嗘爤涓?
鍦烘櫙锛氬綋浣犲湪椤圭洰涓婁慨鏀逛竴浜涘唴瀹癸紝鑰岀幇鍦ㄤ复鏃剁嚎涓婃湁涓揣鎬ug 闇€瑕佷慨澶嶏紝浣嗘槸浣犱笉鎯宠繖涔堜竴浼氬氨鍒涘缓涓€娆℃彁浜わ紝杩欐椂浣跨敤stash锛屽皢淇敼淇濆瓨銆傜瓑Bug 淇敼瀹屽悗锛屽啀娆″垏鍥炲師鍒嗘敮锛屼粠鍫嗘爤涓仮澶嶅垰鍒氳串钘忕殑鍐呭銆?/p>
璐棌鍐呭锛屾坊鍔犲娉ㄤ俊鎭紝鏂逛究浠ュ悗鏌ユ壘锛?/p>
git stash save <message> // message锛氬娉ㄤ俊鎭紙鍙€夊弬鏁颁笅锛?or
git stash //涓嶆坊鍔犲娉ㄤ俊鎭?/code>
鏌ョ湅璐棌鍒楄〃锛?/p>
git stash list
鏌ョ湅璐棌鐨勫唴瀹瑰彉鏇达紝榛樿鏄剧ず绗竴涓紝濡傛灉鏌ョ湅鍏朵粬鐨勮串钘忥紝鍦╯tash${num}锛宯um浠h〃鍝釜璐棌锛?/p>
git show stash //榛樿绗竴涓紝绱㈠紩涓?锛?git show stash@{1} // 鏌ョ湅绗簩娆¤串钘忕殑鍐呭鏈夊摢浜?/code>
搴旂敤鏌愪釜璐棌锛屼絾涓嶄細鍦ㄥ爢鏍堝垪琛ㄤ腑鍒犻櫎锛岃繖閲岀殑绱㈠紩浠?寮€濮嬶紱娉細0鏈€鏂扮殑涓€娆′繚瀛橈紝1涓婁竴娆¤串钘忥紝浠ユ绫绘帹锛?/p>
git stash apply // 榛樿浣跨敤鏈€杩戜竴娆$殑璐棌锛岀浉褰撲簬 git stash apply stash${0}
git stash apply stash${1} // 浣跨敤绗簩涓串钘?/code>
搴旂敤鏌愪釜璐棌锛屽苟涓斾粠鍫嗘爤鍒楄〃涓垹闄わ紱
git stash pop stash@{2} // 浣跨敤绗?涓串钘?骞朵笖鍒犻櫎浠庣紦瀛樹腑鍒犻櫎
灏嗕箣鍓嶇殑鏌愭璐棌浠庡垪琛ㄤ腑鍒犻櫎锛?/p>
git stash drop stash@{2} // 涓㈠純鍒楄〃涓殑绗?娆¤串钘?/code>
鍒犻櫎鎵€鏈夎串钘忕殑stash锛?/p>
git stash clear
鎬荤粨
鏈€鍚?/h4>
濡傛灉鍠滄鎴栧浣犳湁鐢紝閭e氨鐐逛釜璧炲憲锛堭煈嶐煈嶐煈嶏級锛?(鈺碘暟)(鈺碘暟)(鈺碘暟)銆?/p>
以上是关于涓€瀛﹀氨浼氱殑git鍛戒护的主要内容,如果未能解决你的问题,请参考以下文章