git branch git tag git merge git checkout git fetch 常用命令

1.配置git信息
 git config --global user.name 'demo'   //設置用戶名
 git config --global user.email '[email protected]'  //設置用戶郵箱
 ssh-keygen -t rsa -C [email protected] //生成SSH加密密鑰
----------
2.建立版本倉庫
git init //當前路徑建立版本倉庫
1)屬於已知遠程倉庫路徑,那麼直接clone下代碼,後面再執行其他git操作。
2)先本地建立了倉庫,後面要綁定到遠程倉庫路徑
git remote add origin https://github.com/xxx.git
----------
3.分支  branch
git branch  [branchname] //新建分支
git checkout [branchname] //切換到分支
git checkout -b  [branchname] // ** -B表示強制創建新的分支覆蓋原同名分支;如果分支存在則只切換分支,若不存在則創建並切換到分支   
git checkout -b [branchname] [tagname] //創建一個基於指定tag的分支
git push origin [branchname] //提交本地分支到遠程
git branch //查看本地分支
git branch -a //查看本地及遠程分支
git branch -d [branchname] // 刪除分支
git branch -D [branchname] // 強制刪除分支
git push origin :[branchname] // 刪除線上分支
git branch -m oldName newName //修改分支名稱
----------
4.標籤 tag
git tag -a [tagname] -m 'msg備註' //當前分支創建附註標籤
git push origin [tagname]  //提交單個標籤到遠程
git push origin –tags // 本地所有標籤一次性提交到遠程
git tag -d [tagname] // 刪除標籤
git tag //查看所有標籤
git checkout [tagname] //切換到標籤
git checkout -b [branchname] [tagname] //創建一個基於指定tag的分支
----------
5.git checkout
1)操作文件
git checkout filename //撤銷單個文件的修改
git checkout .  //撤銷所有變化文件的修改
2)操作分支、標籤
git checkout [tagname]|[branchname] //切換到標籤|分支
----------
5.pull  push
git add filename //提交單個文件到暫存區
git add . //提交所有變化文件到暫存區
git commit -m 'msg' // 提交本地修改到本地倉庫
git pull // 更新默認分支代碼
git pull origin [branchname] //更新指定分支代碼
git push //推送到默認分支
git push --set-upstream origin master //首次指定默認push 主幹道
git push origin [branchname] // 推送到指定分支
git status  //查看當前git 狀態,會對下一步的操作給出提示
----------
撤銷已add的文件:
git reset HEAD <file>... 
其實可以在git status後看到git的命令提示的
----------
撤銷commit但未push的文件:
1、找到之前提交的git commit_id 
git log  查看要撤銷的commit_id
2、git reset –hard commit_id 
完成撤銷,同時將代碼恢復到前commit_id對應的版本 
3、git reset commit_id 
完成Commit命令的撤銷,但是不對代碼修改進行撤銷,可以重新git commit提交對本地代碼的修改
----------
6. merge
合併分支
git merge [branchname] //把branchname分支代碼合併到當前分支
一般無衝突的話,會自動合併。如果有代碼衝突的話,會自動進入vim狀態,這時候輸入:wq(保存並退出),然後再針對有衝突的地方逐個修改,最後再做add、commit、push等操作。
----------
7. fetch
在遇到需要更新branch|tag的時候敲入 git fetch --all 即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章