git工作中常見、實用命令

一、提交代碼的流程

1、master分支發佈

①、git commit -am  提交你的分支

②、git checkout master 切換

③、git pull origin master 最新的代碼

④、git merge zxy 你的分支

⑤、如果有衝突,解決衝突。git commit -m ''

⑥、git push origin master 推送到遠程

2、分支開發,分支發佈(zxy)

①、本地更改完之後,git stash

②、git pull 或者 git pull origin master

③、git stash pop

④、解決衝突

⑤、git commit -am  ''

⑥、git push origin  zxy 推送到遠程

二、git個人開發的注意事項

1、一般接觸到的分支有三個‘線上’、‘master’、‘develop’

①、基於‘master’分支新建分支,本地分支(除了開發內容)要與‘master’保持一致。

②、項目區分分支,每個項目都有基於‘master’新建的分支。

③、在本地分支只能操作,git pull origin master。

④、上線,將自己的分支合併到‘master’。

⑤、直接拉去遠程的分支:git fetch origin develop:test

2、常用命令

①、修改本地分支名稱,git branch -m 原分支名 新分支名

②、刪除本地分支,git branch -D 分支名

       刪除遠程分支:git branch -a ;查看本地以及遠程所有分支;git push origin --delete  分支名;

③、在合併一個分支後發現有很多衝突,如果想放棄這次合併,git merge --abort

④、在master上拉一個分支並切入這個分支,git checkout -b  分支名稱

⑤、退回到上一次commit的版本,git log,git reset --hard commit_id(版本的號)

⑥、修改剛剛commit的備註,git commit --amend

3、問題解決方案

1 git pull遇到錯誤:error: Your local changes to the following files would be overwritten by merge:

方法1:如果你想保留剛纔本地修改的代碼,並把git服務器上的代碼pull到本地(本地剛纔修改的代碼將會被暫時封存起來)

git stash
git pull origin master
git stash pop

如此一來,服務器上的代碼更新到了本地,而且你本地修改的代碼也沒有被覆蓋,之後使用add,commit,push 命令即可更新本地代碼到服務器了。

方法2、如果你想完全地覆蓋本地的代碼,只保留服務器端代碼,則直接回退到上一個版本,再進行pull:

git reset --hard
git pull origin master

2 在git push origin master時出現以下這個問題時:

error: failed to push some refs to '[email protected]:yangchao0718/cocos2d.git
hint: Updates were rejected because the tip of your current branch is behin
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

出現錯誤的主要原因是github中的README.md文件不在本地代碼目錄中 可以通過如下命令進行代碼合併【注:pull=fetch+merge]

git pull --rebase origin master

git push -u origin master

3 如果出現這樣的錯誤:The file will have its original line endings in your working directory.

解決辦法:

git rm -r --cached ./
git config core.autocrlf false
git add ./

4 git出現這樣的錯誤:Git master branch has no upstream branch

$> git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
 
    git push --set-upstream origin master

原因分析:沒有將本地的分支與遠程倉庫的分支進行關聯。出現這種情況主要是由於遠程倉庫太多,且分支較多。在默認情況下,git push時一般會上傳到origin下的master分支上,然而當repository和branch過多,而又沒有設置關聯時,git就會產生疑問,因爲它無法判斷你的push目標。

解決: 方法一:(遠程分支存在的情況才能使用)

# 查看要指向的 repository
git remote -v
 
# 查看所有分支
git branch -a

git push --set-upstream origin master
# master: 遠程branch
# oringin: 在clone遠程代碼時,git爲你創建的指向這個遠程代碼庫的標籤,它指向repository。

方法二:根據需要,替換origin和master,此方法的好處是即使遠程沒有你要關聯的分支,它也會自動創建一個出來,以實現關聯。

git push -u origin master

 

5 出現這個錯誤:! [rejected] master -> master (fetch first)

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:bboyHan/golang-data.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因分析:沒有同步遠程的master 解決:

git pull origin master

6 Git出現failed to push some refs to

描述:

$ git push -u origin master
To [email protected]:******/Demo.git
 ! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方法:

(1).使用強制push的方法:
$ git push -u origin master -f 
這樣會使遠程修改丟失,一般是不可取的,尤其是多人協作開發的時候。
(2).push前先將遠程repository修改pull下來
$ git pull origin master
$ git push -u origin master
(3).若不想merge遠程和本地修改,可以先創建新的分支:
$ git branch [name]
然後push
$ git push -u origin [name]

7 Git出現fatal: refusing to merge unrelated histories的時候

描述:這是從遠程庫pull項目,合併文件發生的異常

解決方案:在pull的時候添加 --allow-unrelated-histories。

$ git pull origin master ----allow-unrelated-histories

這個功能是可以讓大家不要把倉庫上傳錯了,如果會加上這個代碼,那麼就是自己確定了上傳。之前很容易就把代碼傳錯了,現在可以看到,如果上傳的不是之前的,那麼就需要加代碼。

8 當Git出現error:src refspec master does not match any 錯誤時:

描述:在push項目的時候,引發該異常。 原因分析:目錄中沒有文件,空目錄是不能提交上去的,獲取沒有add、commit文件直接進行push了。 解決方案:

touch README
git add README 
git commit -m 'first commit'
git push origin master

9 git時出現fatal: Authentication failed for 'https://github.com/ ...

描述:使用的https提交,在用SourceTree提交代碼時候發生錯誤,返回的錯誤提示說:fatal:Authentication failed for'https://github.com/... 解決方案:重新執行Git config命令配置用戶名和郵箱即可:

git config -–global user.name "xxx" 
git config –-global user.email "[email protected]"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章