Git命令總結

2021-08-01 23:40:14 字數 2077 閱讀 5123

建立版本庫

git init

git add readme.md

git commit -m 「first commit」

git remote add origin your_git_url //將本地的版本庫與遠端分支關聯

git push -u origin master //將本地的版本庫與遠端分支關聯

建立分支

git branch //檢視本地所有分支

git branch -a //檢視遠端所有分支

git branch branch_name //新建乙個名為branch_name的本地分支

git checkout branch_name //切換到branch_name分支上

合併分支

假設當前專案有分支1和分支2兩個分支,現在想把分支1的內容合併到分支2中

git checkout branch2 //一定要切換到目標分支上

git merge branch1

提交修改

git pull //拉取最新的**避免上傳出現衝突

git add .

git commit -m 「your_commit」

git push

刪除檔案或者分支

git rm file_name //刪除某個檔案(徹底刪除,物理上和索引上都會刪除)

git rm -r directory _name //徹底刪除某個資料夾下的所有檔案

git rm - -cached file_name // 僅在索引上刪除某個檔案(也就是不讓git追蹤它了)

git branch -d branch_name 刪除本地分支

git push origin --delete branch_name 刪除遠端分支(如果出現錯誤請使用下面的命令 git push origin :refs/heads/branch_name )

放棄修改

1 如果只是修改檔案但是還沒有使用git add

git checkout -- .//這句話是要放棄所有工作區修改,慎用!!!

git checkout -- //這句話是放棄file檔案的修改

2 git add了但是還沒有使用git commit

git reset head //撤銷file檔案提交

git reset head^ //撤銷所有的提交

3 git commit了但是還沒有使用git push

git reset --soft head^   ^為最近一次 ^2為上上次  head可以變為指定版本號

--mixed 不刪除工作空間改動**,撤銷commit,並且撤銷git add .

--soft 不刪除工作空間改動**,撤銷commit,不撤銷git add .

--hard 刪除工作空間改動** 工作區回退到最近一次commit狀態

git reset --hard commitid 回到某乙個提交版本號

4 git push就真的無藥可救了嗎?

假如你上傳完畢的時候發現你的**居然有個錯誤,那你心情一定是崩潰的

解決辦法如下兩種:

1 本地修改為正確然後上傳正確的

2 git push -f origin master//強制將當前的分支作為最新的覆蓋掉遠端分支,這個需要許可權,一般強制push都會遇到許可權受阻

git命令總結

git config global user.name fangting git config global user.email 126.com git stash list 顯示git棧內的所有備份,可以利用這個列表來決定哪個地方恢復。git stash clear清空git棧。git stat...

Git命令總結

git 操作步驟 1.新建倉庫mkdir mygit 建立倉庫目錄 cd mygit 進入mygit進行操作 pwd 顯示mygit的路徑,此步驟可以不要 2.初始化git init 將目錄轉為git可以管理的倉庫 3.新增檔案新建乙個檔案 如txt檔案 一定要放到剛才新建的目錄下 子目錄也行 這裡...

git命令總結

1.使用repo命令批量提交多個庫的修改 repo forall project1 project2 c git add a repo forall project1 project2 c git commit m commit comment here repo forall project1 p...