一、未使用 git add 缓存代码
可以使用git checkout -- filepathname
(比如: git checkout -- readme.md
,不要忘记中间的 “--” ,不写就成了切换分支了!!)。放弃所有的文件修改可以使用 git checkout .
命令。
二、已经使用了 git add 缓存了代码
可以使用 git reset HEAD filepathname
(比如: git reset HEAD readme.md
)来放弃指定文件的缓存,放弃所以的缓存可以使用git reset HEAD .
命令。
三、已经用 git commit 提交了代码
可以使用git reset --hard HEAD^
来回退到上一次commit的状态。此命令可以用来回退到任意版本:git reset --hard commit id
你可以使用 git log
命令来查看git的提交历史。
回到了如(二)所示的状态。继续用(二、一)中的操作,就可放弃本地的修改
场景:已经commit提交了,但是后悔了,或者是你提交之前没有git pull拉取代码,导致有冲突,所以我们先撤回提交,这里有两个方法:git reset –hard 5029f0cc08cf
和 git reset --soft 5029f0cc08cf
,下面详细说一下两个方法的应用:
git reset --soft <commit_id>:回撤commit到之前的某个commit_id版本,再git status查看,可以看到已经回撤,并且保留了之前修改。
git reset –hard <commit_id>:这种方式不推荐,他也是撤销,但是并不会保留之前的修改。再git push -f ,这样会清除掉提交的记录。除非你真的不想要你刚刚commit的代码,否则,使用这个命令会让你提交的代码全部清除,你之前的工作也就全白干了。用错了这个命令,恐怕哭都找不着调。所以,非常不推荐这个命令,用的话也一定要慎之又慎。
最后,执行git reset --soft <commit_id>之后,强制推送代码到分支:
git commit -m 'feat: test'
git push origin feat/blabla --force
四、拉取代码冲突
方法1:保留本地修改的代码,并把git服务器上的代码pull到本地。这种情况下代码中会有<<<<<<< Updated upstream提示,然后手动整合代码再上传。
git stash #暂存本地的代码
git pull #拉取远程代码
git stash pop #取出本地代码合并,之后会有<<<<<<< Updated upstream等东西出现,手动修改之后再push
git add .
git commit -m "xxx"
git push
方法2:覆盖本地的代码,只保留服务器端代码。这种情况下可以先把自己修改的地方记录在记事本中,拉取之后再合入自己的代码。
git reset --hard #重置到上个版本
git pull #拉取代码
五、提交的时候报错
1、代码已经pull过了,但是在push的时候提交报以下错误:
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.
提示:更新被拒绝,因为远程包含您所做的工作
提示:本地没有。这通常是由另一个存储库推入引起的
提示:到相同的引用。您可能想首先集成远程更改
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.
致命的:需要指定如何调和不同的分支。
3、我们查看代码状态
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'已经分叉,分别有1个和1个不同的提交。我们需要变基,将本地master最新的代码合进本地的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 就是 对某次 commit 的一个标识,相当于起了一个别名。我们的用处就是在项目中引入组件的特定tag来集成特定的某些版本功能。如下:
pod 'TYTNetworking', :git => 'git@192.168.22.120:iOS-Components/TYTNetworking.git', :tag => '0.2.0-beta'
1、查看tag
git tag : 直接列出所有的标签
git tag -l xxxx : 可以根据 xxxx 进行标签的筛选
2、查看tag提交的信息
git show 标签名
3、创建标签
创建标签有两种:轻量标签、附注标签
3.1 轻量标签
git tag 标签名 : 直接给当前的提交版本创建一个【轻量标签】
git tag 标签名 提交版本号 :给指定的提交版本创建一个 【轻量标签】,指定的版本就是commit_id
git tag 0.2.0-beta
git tag 0.2.0-beta 9c659159e63743657d6ad506e2b200b17f232fbd //指定版本创建标签
3.2 附注标签
附注标签就是我们在标签后面添加相应的注释描述,方便一目了然的知道我们的每个标签都做了什么,有两种方式,如下:
git tag -a 标签名称 -m 附注信息
git tag -a 标签名称 提交版本号 -m 附注信息
-a : 理解为 annotated 的首字符,表示 附注标签
-m : 指定附注信息
git tag -a 标签名称 -m 附注信息 :直接给当前的提交版本创建一个 【附注标签】
git tag -a 标签名称 提交版本号 -m 附注信息 :给指定的提交版本创建一个【附注标签】
git tag -a 0.2.0 -m "更新图片资源管理"
git tag -a 0.2.0 9c659159e63743657d6ad506e2b200b17f232fbd -m "中间类的迁移"
3.3 删除标签
git tag -d 标签名称 : 删除指定名称的标签
git tag -d 0.2.0
3.4 推送到远端仓库
默认情况下,git push 命令并不会把标签推送到远程仓库中。
使用下面的方法:
$ git push origin 标签名称
$ git push origin --tags
git push origin 标签名称 : 将指定的标签上传到远程仓库
git push origin --tags : 将所有不在远程仓库中的标签上传到远程仓库
git push origin 0.2.0
git push origin --tags
3.5 删除远程仓库的标签
下面两个命令都是删除远程仓库中的指定标签:
git push origin :regs/tags/0.2.0
git push origin --delete 0.2.0
git tag 一文真正的搞懂git标签的使用!!!!!!!!!!
七、添加gitignore文件
gitignore的好处就是我们可以把不需要提交的文件都放到里面,比如Pods中的第三方库就没有必要提交到gitee库中。拉下来代码之后,直接pod install就行了。
创建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