Git日常命令使用

個人在開發(fā)中整理常用的git命令,相信很多人會需要到的。

全局配置信息:

    git config --global user.name "Your name"
    git config --global user.email "email@example.com"

初始化項目

        git init 
        git add . 
        git commit -m 'init project'
        git remote add origin newurl
        git pull origin master ----allow-unrelated-histories  //遠程已經(jīng)有文件情況下
        git push -u origin master

創(chuàng)建新的分支

    git branch -b 1.0.1
    git push origin 1.0.1 提交遠程分支

刪除遠程分支:

    git push origin --delete <branchName>
    git init 提交遠程
    添加遠程版本庫,如果版本庫不存在,則會創(chuàng)建版本庫
    git remote add origin https://git.coding.net/moyuanhui/Test.git
    更新本地代碼(遠程可能有些代碼本地是沒有的),origin是剛才創(chuàng)建的版本庫
    git pull origin master
    提交本地代碼
    git push origin master  

創(chuàng)建版本庫

    git init
    git add filename
    git commit -m 'message'

查看文件不同

    git diff filename.txt 是工作區(qū)和暫存區(qū)的比較
    git diff --cached 是暫存區(qū)和分支比較
    git diff HEAD -- filename.txt 比較工作區(qū)和版本庫的文件區(qū)別
    git log 版本歷史
    git reflog 查看記錄每一次命令
    git reset --hard HEAD^ 回退上一個版本
    git reset --head commitId 回退制定版本

撤銷暫存區(qū)的文件修改

    git reset HEAD readme.txt
    git checkout -- readme.txt

從版本庫中刪除文件

    git rm test.txt
    git commit -m 'remove test.txt'

創(chuàng)建分支

    git checkout -b newBranch 創(chuàng)建newBranch分支
    ==
    git branch newBranch
    git checkout newBranch

查看當前分支

    git branch

合并分支

    git merge dev 表示合并dev分支到當前分支上
    查看分支:git branch

    創(chuàng)建分支:git branch <name>

    切換分支:git checkout <name>

    創(chuàng)建+切換分支:git checkout -b <name>
    
    合并某分支到當前分支:git merge <name>

    刪除分支:git branch -d <name>

工作現(xiàn)場儲藏:

    git stash
    git checkout -b issue-101
    git stash list    查看stash
    git stash apply stash@{0} 恢復指定的stash

創(chuàng)建標簽:

    git tag v1.0  創(chuàng)建標簽
    git tag 查看現(xiàn)有標簽
    git log --pretty=oneline --abbrev-commit 查看提交歷史記錄
    git tag v1.0 2342534534 對已經(jīng)提交的打上標簽
    git show v1.0 可以用git show <tagname>查看標簽信息:
    git tag -a v0.1 -m "說明文字"  創(chuàng)建帶有說明的標簽
    git tag -d v0.1 刪除標簽
    git push origin v1.0 推送某個標簽到遠程
    git push origin --tags 推送所有標簽到遠程

刪除遠程標簽:

    git tag -d v1.0 先刪除本地標簽
    git push origin :refs/tags/v1.0 再刪除遠程標簽

比較兩個分支文件差異

    git diff branch1 branch2 --stat

拉取遠程分支

表示拉取不成功。我們需要先執(zhí)行

git fetch
然后再執(zhí)行

git checkout -b 本地分支名 origin/遠程分支名

本文版權歸作者和博客園共有,歡迎轉(zhuǎn)載,須保留此段聲明,并給出原文鏈接,謝謝!
如果閱讀了本文章,覺得有幫助,歡迎點擊右下角推薦

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容