git基本命令

随时补充

920 创建: 2019/9/20 08:00 更新: 2022/5/28 12:50 本文总阅读量

基本

git中文手册
易百

当前代码放入缓存区做准备:

git add ./

提交到本地并注释(wu):

git commit -m wu

推送到远程服务器:

git push origin yangzhou

切换分支:

git checkout yangzhou

切换之后更新一下分支:

git pull origin yangzhou

注意:切换分支前先提交一下

拉取远程分支(拉取所有新的)

git fetch

拉取远程制定分支

git fetch origin XXX

SPM

xcodebuild -resolvePackageDependencies

xcodebuild -resolvePackageDependencies -scmProvider system

新用户初次

配置

//新用户 初次连接仓库
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"

新工程

  1. 创建桌面文件夹 cd ……
  2. 初始化 git init
  3. 放入缓存区 git add ./
  4. 第一次提交 git commit -m first
  5. 关联仓库 git remote add origin 远程地址
  6. 强制提交 git push -f origin master

克隆某一个

cd /Users/apple/Desktop/直播
git clone -b JinShanThird 远程仓库地址

将dev代码合并到master

## 1.切换到dev分支(如果还没有在dev分支上):
git checkout dev

## 2.拉取master分支最新的更改:
git pull origin master

## 3.解决合并冲突(如果有的话):

## 4.切换到master分支:
git checkout master

## 5.合并dev分支到master分支:
git merge dev

## 6.次解决合并冲突(如果有的话):

## 7.提交合并后的更改:
git commit -m "merge dev to master"

## 8.将更改推送到远程master分支:
git push origin master

dev的某一次commit提交到master

  • 说明:dev分支有开发并且又多次commit,只想将某一次commit合并到master
## 1. 切换到目标分支-`master`
git checkout master

## 2. 找到要`cherry-pick`的提交:使用以下命令查看分支B的提交历史,并找到您想要合并的提交的哈希值
git log dev

## 3. `cherry-pick`指定的提交:使用`cherry-pick`命令将特定提交合并到目标分支(例如分支`master`)
git cherry-pick efcfb5b89a6cc45e0f5d291689d0923f7e67a230

## 4.解决可能出现的冲突
git add <conflicted-file>
git cherry-pick --continue

例如日志:
Auto-merging YBLive.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in YBLive.xcodeproj/project.pbxproj
Auto-merging YBLive/公共方法类/YBToolClass.m
Auto-merging YBLive/直播(开始观看直播)/Agora/YBAgoraManager.m
Auto-merging YBLiveScreen/SampleHandler.m
CONFLICT (content): Merge conflict in YBLiveScreen/SampleHandler.m
error: could not apply efcfb5b8... 客户反馈功能补充以及bugfix

找到 CONFLICT 标记的文件,手动修改 然后 
git add ./
git commit -m 'xxx'
git cherry-pick --continue

## 5.完成`cherry-pick`后
git push origin master

cherry-pick 完全日志
2408261


~/Desktop/live YBLive241108 % git cherry-pick efcfb5b89a6cc45e0f5d291689d0923f7e67a230
Auto-merging YBLive.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in YBLive.xcodeproj/project.pbxproj
Auto-merging YBLive/公共方法类/YBToolClass.m
Auto-merging YBLive/直播(开始观看直播)/Agora/YBAgoraManager.m
Auto-merging YBLiveScreen/SampleHandler.m
CONFLICT (content): Merge conflict in YBLiveScreen/SampleHandler.m
error: could not apply efcfb5b8... 客户反馈功能补充以及bugfix
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".



==========================


Last login: Thu Nov  7 14:46:08 on ttys053
~ % ls
Applications            Movies              Public
Desktop             Music               Sites
Documents           OrbStack            i4Remote
Downloads           Pictures            java_error_in_idea.hprof
Library             Postman
~ % cd Desktop/live 
~/Desktop/live NuoFengTai * % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live NuoFengTai * % git add ./
~/Desktop/live NuoFengTai * % git commit -m '客户反馈功能补充以及bugfix'
[NuoFengTai efcfb5b8] 客户反馈功能补充以及bugfix
 15 files changed, 341 insertions(+), 49 deletions(-)
 create mode 100644 "YBLive/\345\212\237\350\203\275/\350\277\236\351\272\246\345\260\217\347\252\227\345\217\243/pk\344\270\273\346\222\255\344\277\241\346\201\257/PKAnchorInfoView.h"
 create mode 100644 "YBLive/\345\212\237\350\203\275/\350\277\236\351\272\246\345\260\217\347\252\227\345\217\243/pk\344\270\273\346\222\255\344\277\241\346\201\257/PKAnchorInfoView.m"
~/Desktop/live NuoFengTai % git push origin NuoFengTai
Enumerating objects: 75, done.
Counting objects: 100% (75/75), done.
Delta compression using up to 8 threads
Compressing objects: 100% (44/44), done.
Writing objects: 100% (44/44), 24.75 KiB | 4.95 MiB/s, done.
Total 44 (delta 35), reused 0 (delta 0), pack-reused 0
To http://192.168.1.55:3000/yunbao/YbLiveiOS.git
   691cca57..efcfb5b8  NuoFengTai -> NuoFengTai
~/Desktop/live NuoFengTai % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live NuoFengTai % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live NuoFengTai % git branch
  BeiJingZhao
  EnShiZhouXiHuKang
  HanXiWangLuo
  JiangSuYunDing
  LiaoNingMa
  MaLaiXiYaBw
* NuoFengTai
  ShanXiShengDu
  ShenYangWeiYouChen
  TengMeiKeJi
  TuErQiEren
  WuHanTengXin
  YBLive230621
  YBLive231114
  YBLive240201
  YBLive240820
  guohualimin
  guohualimin0801
  guohualimin0827
  henankayan
  henankayan0909
~/Desktop/live NuoFengTai % git checkout YBLive240820
Switched to branch 'YBLive240820'
~/Desktop/live YBLive240820 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive240820 % open YBLive.xcworkspace
~/Desktop/live YBLive240820 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive240820 % git checkout -b YBLive241108
Switched to a new branch 'YBLive241108'
~/Desktop/live YBLive241108 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 % git log 
commit 6e689015614832c077149c612257f811073503b1 (HEAD -> YBLive241108, origin/YBLive240820, YBLive240820)
Author: yuan <yzyuan920@163.com>
Date:   Sat Nov 2 16:15:05 2024 +0800

    客户问题同步更新ok

commit 14766bb4fbe38fc2d5d1b2ef99e0f88b870c0643
Author: yuan <yzyuan920@163.com>
Date:   Sat Oct 26 10:56:48 2024 +0800

    腾讯SDK鉴权问题处理

commit bc939d6d030ebc25923d210d8304cfba151a6eca
Author: yuan <yzyuan920@163.com>
Date:   Wed Oct 9 09:15:59 2024 +0800

    pod-pch-update

commit d345996b0106e512d20ddf80644a7e0c1f97a580
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 30 16:00:35 2024 +0800

    聊天室主播端用户列表fix

commit 7016d80ef1981ed16a1f2a7f62d194d26f112459
Author: yuan <yzyuan920@163.com>
Date:   Mon Aug 26 14:29:42 2024 +0800

    用户端观看bugfix

commit caadfa8144b55da8ab9a0c5ffb32f9a7985134e8
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 23 11:33:42 2024 +0800

    问题同步

commit adf1dbaf192d5d52e8bb2de313801a4ace58d80b
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 23 08:38:26 2024 +0800

    腾讯sdk-bugfix

commit 67dc39726ba62ebff453492afa5498e581de115a
Author: yuan <yzyuan920@163.com>
Date:   Wed Aug 21 15:28:37 2024 +0800

    开发ok

commit 5579fb010d8c72c6aeb2c48ff42d9115aa14b77f
~/Desktop/live YBLive241108 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 % open YBLive.xcworkspace
~/Desktop/live YBLive241108 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 % git log NuoFengTai
commit efcfb5b89a6cc45e0f5d291689d0923f7e67a230 (origin/NuoFengTai, NuoFengTai)
Author: yuan <yzyuan920@163.com>
Date:   Fri Nov 8 08:32:28 2024 +0800

    客户反馈功能补充以及bugfix

commit 0dbc5c5b10a9a36c786d034b491c33779d0050b6
Author: yuan <yzyuan920@163.com>
Date:   Mon Nov 4 16:22:08 2024 +0800

    sdk录屏日志

commit 691cca579a537b4d53962e2c49596273fc69e9f1
Author: yuan <yzyuan920@163.com>
Date:   Sat Nov 2 15:58:03 2024 +0800

    客户反馈fix

commit d283e648289a88b09e7ab35aca40d821dba3b0b1
Author: yuan <yzyuan920@163.com>
Date:   Fri Nov 1 10:41:36 2024 +0800

    游客报错

commit 9e0dfff0bced893d8c2c1fc2cc67d279a5547b4b
Author: yuan <yzyuan920@163.com>
Date:   Thu Oct 31 09:13:30 2024 +0800

    封包ok

commit 14766bb4fbe38fc2d5d1b2ef99e0f88b870c0643
Author: yuan <yzyuan920@163.com>
Date:   Sat Oct 26 10:56:48 2024 +0800

    腾讯SDK鉴权问题处理

commit bc939d6d030ebc25923d210d8304cfba151a6eca
Author: yuan <yzyuan920@163.com>
Date:   Wed Oct 9 09:15:59 2024 +0800

    pod-pch-update

commit d345996b0106e512d20ddf80644a7e0c1f97a580
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 30 16:00:35 2024 +0800

    聊天室主播端用户列表fix

commit 7016d80ef1981ed16a1f2a7f62d194d26f112459
Author: yuan <yzyuan920@163.com>
Date:   Mon Aug 26 14:29:42 2024 +0800

    用户端观看bugfix

commit caadfa8144b55da8ab9a0c5ffb32f9a7985134e8
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 23 11:33:42 2024 +0800

    问题同步

commit adf1dbaf192d5d52e8bb2de313801a4ace58d80b
Author: yuan <yzyuan920@163.com>
Date:   Fri Aug 23 08:38:26 2024 +0800

    腾讯sdk-bugfix

commit 67dc39726ba62ebff453492afa5498e581de115a
Author: yuan <yzyuan920@163.com>
Date:   Wed Aug 21 15:28:37 2024 +0800

    开发ok
~/Desktop/live YBLive241108 % git cherry-pick efcfb5b89a6cc45e0f5d291689d0923f7e67a230
Auto-merging YBLive.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in YBLive.xcodeproj/project.pbxproj
Auto-merging YBLive/公共方法类/YBToolClass.m
Auto-merging YBLive/直播(开始观看直播)/Agora/YBAgoraManager.m
Auto-merging YBLiveScreen/SampleHandler.m
CONFLICT (content): Merge conflict in YBLiveScreen/SampleHandler.m
error: could not apply efcfb5b8... 客户反馈功能补充以及bugfix
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
~/Desktop/live YBLive241108 * % ls
Podfile         Pods            YBLive          YBLive.xcworkspace
Podfile.lock        README.md       YBLive.xcodeproj    YBLiveScreen
~/Desktop/live YBLive241108 * % open YBLive.xcworkspace
~/Desktop/live YBLive241108 * % git add YBLiveScreen/SampleHandler.m
~/Desktop/live YBLive241108 * % git cherry-pick --continue
U   YBLive.xcodeproj/project.pbxproj
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
~/Desktop/live YBLive241108 * % git add YBLiveScreen/SampleHandler.m
~/Desktop/live YBLive241108 * % git cherry-pick --continue          
U   YBLive.xcodeproj/project.pbxproj
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
~/Desktop/live YBLive241108 * % git add YBLiveScreen/SampleHandler.m
~/Desktop/live YBLive241108 * % git commit -m '录屏fix'
U   YBLive.xcodeproj/project.pbxproj
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
~/Desktop/live YBLive241108 * % git add ./
~/Desktop/live YBLive241108 * % git commit -m '录屏fix'
[YBLive241108 066a5893] 录屏fix
 Date: Fri Nov 8 08:32:28 2024 +0800
 15 files changed, 369 insertions(+), 70 deletions(-)
 create mode 100644 "YBLive/\345\212\237\350\203\275/\350\277\236\351\272\246\345\260\217\347\252\227\345\217\243/pk\344\270\273\346\222\255\344\277\241\346\201\257/PKAnchorInfoView.h"
 create mode 100644 "YBLive/\345\212\237\350\203\275/\350\277\236\351\272\246\345\260\217\347\252\227\345\217\243/pk\344\270\273\346\222\255\344\277\241\346\201\257/PKAnchorInfoView.m"
~/Desktop/live YBLive241108 % git cherry-pick --continue
error: no cherry-pick or revert in progress
fatal: cherry-pick failed
~/Desktop/live YBLive241108 % git cherry-pick efcfb5b89a6cc45e0f5d291689d0923f7e67a230
Auto-merging YBLive.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in YBLive.xcodeproj/project.pbxproj
Auto-merging YBLive/公共方法类/YBToolClass.m
Auto-merging YBLive/直播(开始观看直播)/Agora/YBAgoraManager.m
Auto-merging YBLiveScreen/SampleHandler.m
error: could not apply efcfb5b8... 客户反馈功能补充以及bugfix
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
~/Desktop/live YBLive241108 * % git add ./
~/Desktop/live YBLive241108 % git cherry-pick --continue                              
On branch YBLive241108
You are currently cherry-picking commit efcfb5b8.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
~/Desktop/live YBLive241108 % git commit -m '合并诺丰泰'
On branch YBLive241108
You are currently cherry-picking commit efcfb5b8.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
~/Desktop/live YBLive241108 % git push origin YBLive241108
Enumerating objects: 67, done.
Counting objects: 100% (67/67), done.
Delta compression using up to 8 threads
Compressing objects: 100% (36/36), done.
Writing objects: 100% (36/36), 23.93 KiB | 4.79 MiB/s, done.
Total 36 (delta 29), reused 0 (delta 0), pack-reused 0
To http://192.168.1.55:3000/yunbao/YbLiveiOS.git
 * [new branch]        YBLive241108 -> YBLive241108
~/Desktop/live YBLive241108 % 

放弃本地、拉取远程最新

git reset --hard origin/master    //origin/master替换为要拉取的远程分支名称
git add ./
git commit -m XXX
git pull

命令行执行 git branch 就进入编辑状态

解决方案:

git config --global core.pager mor

pod更新索引

更新索引、移除索引

pod repo update
pod repo update --verbose //是打印详细信息.
pod install --verbose --no-repo-update

pod repo remove trunk
rm ~/Library/Caches/CocoaPods/search_index.json

# 清楚缓存
pod cache clean --all

查询命令

grep -r --colour "tx123" /Users/yb007/Library/MobileDevice/Provisioning\ Profiles

移除

lipo -remove i386 tracking -o tracking 
lipo -remove x86_64 tracking -o tracking 

记录

git log
git log --oneline
git reflog
git reset --hard xxx

#暂存区和远程
git show xxx
git show -s

#工作区和暂存区状态
git status

#工作区和暂存区不同
git diff

#本地和远程比较
git branch -a
git diff --stat --color xxx  
确认是否已提交远程仓库

如何确定代码已经提交远程:

  1. 首先根据git status/git diff判定工作区暂存区是否同步;
  2. 利用git show -s再次判定暂存区是否提交远程;

注意:

  1. git status在主干可以确定是否是否有未提交到远程的代码;在子分支不可以区分;
  2. 不能直接使用git show -s来判断是否提交远程,因为有可能工作区的改动都没有同步到暂存区;因此需要先执行第一步.
  • 关于git status的总结:
    – 主干分支提示能够区分是否有需要提交,但未提交远程的改动
    //未提交  
    blog [master] % git status  
    On branch master  
    Your branch is ahead of 'origin/master' by 1 commit.  
    (use "git push" to publish your local commits)    
    //已提交  
    blog [master] % git status  
    On branch master  
    Your branch is up to date with 'origin/master'.  
    

    – 在子分支会提示如下信息无法区分是否有需要提交远程子分支的改动

    YBHiMo [YBLove220421] % git status  
    On branch YBLove220421
    nothing to commit, working tree clean
    
  • 关于git show -s的总结:
    未提交远程状态:(HEAD -> YBLove220421)
    YBHiMo [YBLove220421] % git show -s
    commit d3211c61865ef7437953643fe66cc86263f73dbf (HEAD -> YBLove220421)
    Author: yuan <yzyuan920@163.com>
    Date:   Fri May 27 10:40:31 2022 +0800
    
      刘海屏水印位置修复
    

    已提交远程状态:(HEAD -> YBLove220421, origin/YBLove220421)

    YBHiMo [YBLove220421] % git show -s
    commit d3211c61865ef7437953643fe66cc86263f73dbf (HEAD -> YBLove220421, origin/YBLove220421)
    Author: yuan <yzyuan920@163.com>
    Date:   Fri May 27 10:40:31 2022 +0800
    
      刘海屏水印位置修复
    

更换、检查远程链接方式

// 将https更换为ssh
git remote set-url origin git@github.com:xxx/xxx.git

// 检查
git remote -v

通信协议

拉取git仓库提示:

HTTP/2 stream 1 was not closed cleanly before end of the underlying stream.

可改为1.1

git config --global http.version HTTP/1.1