Git提交代碼相關

git config --global user.name 'xueshanshan'  //git初次配置
git config --global user.email '[email protected]'


git cherry-pick commitid  //在第二個分支中把第一個分支的某次提交拉過來
git commit --amend  //補充上次提交

首先,可以試圖用git push origin <branch-name>推送自己的修改;
如果推送失敗,則因爲遠程分支比你的本地更新,需要先用git pull試圖合併;
如果合併有衝突,則解決衝突,並在本地提交;
沒有衝突或者解決掉衝突後,再用git push origin <branch-name>推送就能成功!

如果git pull提示no tracking information,則說明本地分支和遠程分支的鏈接關係沒有創建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>。
這就是多人協作的工作模式,一旦熟悉了,就非常簡單。


git branch  //查看所有分支
git branch new_branch   //在當前分支的基礎上,創建新分支
git branch new_branch commitId  //在當前分支基礎上,以某個commit創建新分支

git checkout <branch>  //切換分支,如果<branch>不存在,將會根據同名的遠程分支創建新的本地分支,如果找不到重名的,將會報錯,如果本地分支存在,則直接切換
也可以使用 git checkout -b <local_branch_name> remotes/origin/dev-3.3  //明確指定哪個遠程
git checkout --merge <branch>  //在切換分支的時候,將當前分支修改的內容一起打包帶走

重命名本地分支
git branch -m old_local_branch_name new_local_branch_name 

重命名遠程分支
git branch -m old_local_branch_name new_local_branch_name   //step 1:先重命名本地分支
git push origin :old_local_branch_name   //step 2:刪除遠程分支
git push origin new_local_branch_name  //step 3:重新推送新的本地分支

刪除分支
刪除的分支不是當前正在打開的分支  git branch -d <branch_name>
刪除正在打開的分支  git branch -D <branch_name>
刪除遠程分支 git branch -r -d origin/<branch_name>  //只是刪除本地索引,並不是真正刪除遠程分支內容
真正刪除遠程分支 git push origin :<branch_name>



stash命令
git stash可用來暫存當前正在進行的工作,比如想pull最新代碼,又不想添加新的commit,或者另外一種情況,爲了fix一個緊急的bug,先stash,是返回到上一次commit,改完bug後再stash pop,繼續原來的工作
基礎命令:
$git stash
$do some work  //要做的事情
$git stash pop

進階:git stash save "描述"
當多次使用git stash命令後,棧裏會有好多未提交的代碼,
git stash list可以將當前的棧信息打出來,只需要找到對應的版本號,如 git stash apply stash@{1} 就能將對應的工作取出來
git stash drop stash@{1}  //移除對應stash
git stash clear  //刪除所有緩存stash
git stash bransh //從stash創建分支


add完某個文件後將其從暫存區移除:
git rm --cached a.txt

git checkout -- a.txt撤銷命令含義:兩種情況
1 a.txt修改了,但是還沒有add到暫存區  撤銷後就回到和版本庫一模一樣的狀態
2 a.txt修改了,並且add到了暫存區,又做了修改,撤銷修改就回到添加到暫存區後的狀態

add完某個文件後想放棄add 並丟棄修改
先使用 git reset HEAD a.txt
然後   git checkout -- a.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章