git放棄本地修改&拉取代碼沖突&tag標(biāo)記&gitignore

一、未使用 git add 緩存代碼

可以使用git checkout -- filepathname (比如: git checkout -- readme.md,不要忘記中間的 “--” ,不寫就成了切換分支了??!)。放棄所有的文件修改可以使用 git checkout . 命令。

二、已經(jīng)使用了 git add 緩存了代碼

可以使用 git reset HEAD filepathname (比如: git reset HEAD readme.md)來放棄指定文件的緩存,放棄所以的緩存可以使用git reset HEAD . 命令。

三、已經(jīng)用 git commit 提交了代碼

可以使用git reset --hard HEAD^來回退到上一次commit的狀態(tài)。此命令可以用來回退到任意版本:git reset --hard commit id
你可以使用 git log命令來查看git的提交歷史。
回到了如(二)所示的狀態(tài)。繼續(xù)用(二、一)中的操作,就可放棄本地的修改

場景:已經(jīng)commit提交了,但是后悔了,或者是你提交之前沒有g(shù)it pull拉取代碼,導(dǎo)致有沖突,所以我們先撤回提交,這里有兩個(gè)方法:git reset –hard 5029f0cc08cfgit reset --soft 5029f0cc08cf,下面詳細(xì)說一下兩個(gè)方法的應(yīng)用:
git reset --soft <commit_id>:回撤commit到之前的某個(gè)commit_id版本,再git status查看,可以看到已經(jīng)回撤,并且保留了之前修改。
git reset –hard <commit_id>:這種方式不推薦,他也是撤銷,但是并不會(huì)保留之前的修改。再git push -f ,這樣會(huì)清除掉提交的記錄。除非你真的不想要你剛剛commit的代碼,否則,使用這個(gè)命令會(huì)讓你提交的代碼全部清除,你之前的工作也就全白干了。用錯(cuò)了這個(gè)命令,恐怕哭都找不著調(diào)。所以,非常不推薦這個(gè)命令,用的話也一定要慎之又慎。
最后,執(zhí)行g(shù)it reset --soft <commit_id>之后,強(qiáng)制推送代碼到分支:

git commit -m 'feat: test'
git push origin feat/blabla --force

四、拉取代碼沖突

方法1:保留本地修改的代碼,并把git服務(wù)器上的代碼pull到本地。這種情況下代碼中會(huì)有<<<<<<< Updated upstream提示,然后手動(dòng)整合代碼再上傳。

git stash #暫存本地的代碼
git pull #拉取遠(yuǎn)程代碼
git stash pop #取出本地代碼合并,之后會(huì)有<<<<<<< Updated upstream等東西出現(xiàn),手動(dòng)修改之后再push

git add .
git commit -m "xxx"
git push

方法2:覆蓋本地的代碼,只保留服務(wù)器端代碼。這種情況下可以先把自己修改的地方記錄在記事本中,拉取之后再合入自己的代碼。

git reset --hard #重置到上個(gè)版本
git pull #拉取代碼

五、提交的時(shí)候報(bào)錯(cuò)

1、代碼已經(jīng)pull過了,但是在push的時(shí)候提交報(bào)以下錯(cuò)誤:

zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git push
To 192.168.2.20:iOS/client_program.git
 ! [rejected]          dev_6270 -> dev_6270 (fetch first)
error: failed to push some refs to '192.168.2.20:iOS/client_program.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.

提示:更新被拒絕,因?yàn)檫h(yuǎn)程包含您所做的工作
提示:本地沒有。這通常是由另一個(gè)存儲(chǔ)庫推入引起的
提示:到相同的引用。您可能想首先集成遠(yuǎn)程更改
2、再次按照提示pull

zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git pull
remote: Counting objects: 28, done.
remote: Compressing objects: 100% (27/27), done.
remote: Total 28 (delta 21), reused 0 (delta 0)
Unpacking objects: 100% (28/28), 10.43 KiB | 427.00 KiB/s, done.
From 192.168.2.20:iOS/client_program
   1a8ca731..c2daa381  dev_6270   -> origin/dev_6270
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

致命的:需要指定如何調(diào)和不同的分支。
3、我們查看代碼狀態(tài)

zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git status
On branch dev_6270
Your branch and 'origin/dev_6270' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

提示你的分支和'origin/dev_6270'已經(jīng)分叉,分別有1個(gè)和1個(gè)不同的提交。我們需要變基,將本地master最新的代碼合進(jìn)本地的branchA分支。完成后,本地的branchA分支是最新的。使用如下命令:
git rebase origin/dev_6270
然后使用
git pull --rebase
最后使用
git push
成功提交!??!

zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git rebase origin/dev_6270
Successfully rebased and updated refs/heads/dev_6270.
zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git pull --rebase
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 9 (delta 7), reused 0 (delta 0)
Unpacking objects: 100% (9/9), 813 bytes | 101.00 KiB/s, done.
From 192.168.2.20:iOS/client_program
   c2daa381..ac56439e  dev_6270   -> origin/dev_6270
Successfully rebased and updated refs/heads/dev_6270.
zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % git push 
Enumerating objects: 79, done.
Counting objects: 100% (79/79), done.
Delta compression using up to 8 threads
Compressing objects: 100% (48/48), done.
Writing objects: 100% (48/48), 15.48 KiB | 3.87 MiB/s, done.
Total 48 (delta 32), reused 0 (delta 0), pack-reused 0
To 192.168.2.20:iOS/client_program.git
   ac56439e..5713ece9  dev_6270 -> dev_6270
zhanyingzhu@zhanyingdeMacBook-Pro-3 client_program % 

六、打tag

tag 就是 對(duì)某次 commit 的一個(gè)標(biāo)識(shí),相當(dāng)于起了一個(gè)別名。我們的用處就是在項(xiàng)目中引入組件的特定tag來集成特定的某些版本功能。如下:

pod 'TYTNetworking', :git => 'git@192.168.22.120:iOS-Components/TYTNetworking.git', :tag => '0.2.0-beta'

1、查看tag

git tag : 直接列出所有的標(biāo)簽
git tag -l xxxx : 可以根據(jù) xxxx 進(jìn)行標(biāo)簽的篩選

2、查看tag提交的信息

git show 標(biāo)簽名
showtag

3、創(chuàng)建標(biāo)簽
創(chuàng)建標(biāo)簽有兩種:輕量標(biāo)簽、附注標(biāo)簽
3.1 輕量標(biāo)簽
git tag 標(biāo)簽名 : 直接給當(dāng)前的提交版本創(chuàng)建一個(gè)【輕量標(biāo)簽】
git tag 標(biāo)簽名 提交版本號(hào) :給指定的提交版本創(chuàng)建一個(gè) 【輕量標(biāo)簽】,指定的版本就是commit_id

git tag 0.2.0-beta
git tag 0.2.0-beta 9c659159e63743657d6ad506e2b200b17f232fbd //指定版本創(chuàng)建標(biāo)簽

3.2 附注標(biāo)簽
附注標(biāo)簽就是我們在標(biāo)簽后面添加相應(yīng)的注釋描述,方便一目了然的知道我們的每個(gè)標(biāo)簽都做了什么,有兩種方式,如下:

git tag -a 標(biāo)簽名稱 -m 附注信息
git tag -a 標(biāo)簽名稱 提交版本號(hào) -m 附注信息

-a : 理解為 annotated 的首字符,表示 附注標(biāo)簽
-m : 指定附注信息
git tag -a 標(biāo)簽名稱 -m 附注信息 :直接給當(dāng)前的提交版本創(chuàng)建一個(gè) 【附注標(biāo)簽】
git tag -a 標(biāo)簽名稱 提交版本號(hào) -m 附注信息 :給指定的提交版本創(chuàng)建一個(gè)【附注標(biāo)簽】

git tag -a 0.2.0 -m "更新圖片資源管理"
git tag -a 0.2.0 9c659159e63743657d6ad506e2b200b17f232fbd -m "中間類的遷移"

3.3 刪除標(biāo)簽
git tag -d 標(biāo)簽名稱 : 刪除指定名稱的標(biāo)簽

git tag -d 0.2.0

3.4 推送到遠(yuǎn)端倉庫
默認(rèn)情況下,git push 命令并不會(huì)把標(biāo)簽推送到遠(yuǎn)程倉庫中。
使用下面的方法:

$ git push origin 標(biāo)簽名稱
$ git push origin --tags

git push origin 標(biāo)簽名稱 : 將指定的標(biāo)簽上傳到遠(yuǎn)程倉庫
git push origin --tags : 將所有不在遠(yuǎn)程倉庫中的標(biāo)簽上傳到遠(yuǎn)程倉庫

git push origin 0.2.0
git push origin --tags

3.5 刪除遠(yuǎn)程倉庫的標(biāo)簽
下面兩個(gè)命令都是刪除遠(yuǎn)程倉庫中的指定標(biāo)簽:

git push origin  :regs/tags/0.2.0
git push origin --delete 0.2.0

git tag 一文真正的搞懂git標(biāo)簽的使用?。。。。。。。。?!
七、添加gitignore文件
gitignore的好處就是我們可以把不需要提交的文件都放到里面,比如Pods中的第三方庫就沒有必要提交到gitee庫中。拉下來代碼之后,直接pod install就行了。
創(chuàng)建gitignore:touch .gitignore
修改gitignore:open .gitignore

# Specify filepatterns you want git to ignore.
# OS X
.DS_Store

# Xcode
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/
*.xccheckout
profile
*.moved-aside
DerivedData
*.hmap
*.ipa

# Bundler
.bundle

# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
# 
# Note: if you ignore the Pods directory, make sure to uncomment
# `pod install` in .travis.yml
#
Pods/
Podfile.lock
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 如果在修改時(shí)發(fā)現(xiàn)修改錯(cuò)誤,而要放棄本地修改時(shí), 一, 未使用 git add 緩存代碼時(shí)。 可以使用git che...
    gurlan閱讀 6,580評(píng)論 0 4
  • 未使用 git add 緩存代碼時(shí)。 可以使用 git checkout -- filepathname (比如:...
    自you是敏感詞閱讀 209評(píng)論 0 1
  • 場景1.本地修改尚未提交到暫存區(qū), 即本地代碼修改還沒有執(zhí)行(git add),使用命令 場景2. 本地修改已經(jīng)...
    焦糖大瓜子閱讀 9,452評(píng)論 0 3
  • 以前用git都是用工androidstudio上自帶的工具,后面發(fā)現(xiàn)很多問題用命令行解決跟方便,但是對(duì)命令行不熟,...
    江中一沙洲閱讀 266評(píng)論 0 1
  • Git使用教程:https://blog.csdn.net/tgbus18990140382/article/de...
    SkTj閱讀 2,942評(píng)論 1 11

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