git 从实践到生产力

承接上一篇 《git 入门篇》,详细讲解 git 命令具体应用。
提示:git 作为基础工具,代码开发 迭代维护非常适用。

文章目录

  • git 从实践到生产力
  • 圈重点 看想学
  • 1. Git 分支管理
    • 1.1 git 分支管理常用命令
    • 1.2 提交记录,代码回滚
    • 1.4 git 代码提交/拉取
    • 1.5 某次提交记录被覆盖或者删除,找回提交记录
    • 1.6 tag(标签)操作
  • 2. git 分支管理高阶操作
    • 2.1 分支合并
    • 2.2 git 远程仓操作
    • 2.3 patch 应用
    • 2.4 git 错误删除后找回提交
    • 2.5 git异常锁库
    • Tips
  • 结语

圈重点 看想学

  1. git 分支合并
  2. git 远程仓 同步 上传
  3. git 分支合并

1. Git 分支管理

1.1 git 分支管理常用命令

① 查看项目的分支

  git branch #本地git branch -r #远程git branch -a #本地和远程

② 切换分支

git swtich branch_name
git checkout -b branch_name #切换到新分支

③ 重命名git本地分支

git branch -m old_local_branch_name new_local_branch_name

④ 重命名远程分支对应的本地分支(先删除再推送新名字分支)

 git push origin_name --delete old_local_branch_name #删除远程分支git branch -m old_local_branch_name new_local_branch_name #重命名本地分支git push origin_name new_local_branch_name #提交新分支

⑤ 删除本地分支

 git branch -d <BranchName>

⑥ 删除远程分支

  git push origin <BranchName> --delete

⑦ 推送本地分支

#默认推送到与本地分支名相同远程分支
git push origin local_branch
#本地推送到指定远程分支
git push origin local_branch:remote_branch

⑧ 提交覆盖更新远程分支

#强制更新分支,当前分支提交
git push origin branch --force
# 强制更新到指定提交点
git push origin HEAD --force

⑨ 删除本地跟踪 remote

 git branch -rd origin/branch

1.2 提交记录,代码回滚

① 查看某个问题修改记录
a)可列出文件的所有改动历史

git log --pretty=oneline path/fileName

b) 与文件名相关的commit记录

git log path/fileName

c) 只看某次提交中的某个文件变化,可以直接加上fileName

git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path/fileName

② 本地代码回退
a)强制回退到某个版本 不保留修改项

git reset commit-id --hard

b)代码软回退 保留修改项

git reset commit-id
git reset commit-id --soft

③ 代码回滚
a) 回滚单次提交 并生成提交记录

git revert commit-id

b) 回滚多个提交

git revert -n commit-id-old commit-id-new
git commit ./ -m "brief"

或者变基更新,生成新 commit

git rebase -i commit-id

c) 代码回滚但不要提交记录
① 回退到指定commit

git reset --hard <commit-id>

② 强制覆盖提交

 git push remote branch --force git push remote local_branch:remote_branch --force```## 1.3 git 添加 / 删除文件
1) 添加文件
git add -A 所有变化文件
-u 添加被修改(modified)和被删除(deleted)文件,不包括新文件(new)
. 添加新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
添加新文件
```bash
git add $(git status -u <path>) #验证无效
git add $(git status -s <path> | awk -F ' ' '{ print $2 }')
  1. 删除文件,
# 删除追踪文件
git rm $(git ls-files --deleted)
# 删除指定目录下的全部文件
git rm $(git ls-files <path> --deleted)
# 删除未被追踪文件
git clean -df
git ls-files -d | xargs git rm
  1. 未跟踪文件添加删除
    显示未跟踪文件
git ls-files --others --exclude-standard

添加未追踪文件

git sstatus --porcelain | grep '^??' | cut -c4- | xargs git add
#或者
git ls-files -z -o --exclude-standard | xargs -0 git add

1.4 git 代码提交/拉取

a) 提交代码到本地

git commit path/file -m "commit log"

推送到远程

git push remote_name local_branch:remoteb_branch

b) 拉取代码到本地

git fetch remote_name
git pull remote_name local_branch:remoteb_branch

1.5 某次提交记录被覆盖或者删除,找回提交记录

a) 查看过往被删提交记录

 git reflog6891e36 HEAD@{0}: reset: moving to 6891e3609ce9dff84ed1233bb21e33b235823684ddfe728 HEAD@{1}: checkout: moving from hra to t6s

b) 恢复指定删除提交

git cherry-pick 6891e36

1.6 tag(标签)操作

a) 显示本地tag

git tag

b) tag 增加

  • 增加标签
git tag tag_name
  • 增加远程标签
git psuh remote_name branch_name tag_name

c) 删除远程tag
Step1: 删除本地tag

git tag -d tagName

Step2: 删除远程tag

git push origin branch :refs/tags/tagName

Tips git异常锁库

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
$ rm .git/index.lock #简单粗暴处理

2. git 分支管理高阶操作

2.1 分支合并

查看当前分支,讲解分支合并两种常用方式 rebasemerge,先来讲 git merge

git branch
* 356x-harmony356x-onlinemaster
# master 分支已经同步到最新代码,当前 356x-harmony 希望合并更新
git merge master

详细的参数可以通过 --help 参数查看

git merge --help
NAMEgit-merge - Join two or more development histories togetherSYNOPSISgit merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit][--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]][--[no-]allow-unrelated-histories][--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...]git merge (--continue | --abort | --quit)DESCRIPTIONIncorporates changes from the named commits (since the time their histories diverged from the current branch) into thecurrent branch. This command is used by git pull to incorporate changes from another repository and can be used by handto merge changes from one branch into another.Assume the following history exists and the current branch is "master":A---B---C topic/D---E---F---G masterThen "git merge topic" will replay the changes made on the topic branch since it diverged from master (i.e., E) until itscurrent commit (C) on top of master, and record the result in a new commit along with the names of the two parent commitsand a log message from the user describing the changes.A---B---C topic/         \D---E---F---G---H masterThe second syntax ("git merge --abort") can only be run after the merge has resulted in conflicts. git merge --abort willabort the merge process and try to reconstruct the pre-merge state. However, if there were uncommitted changes when themerge started (and especially if those changes were further modified after the merge was started), git merge --abort willin some cases be unable to reconstruct the original (pre-merge) changes. Therefore:Warning: Running git merge with non-trivial uncommitted changes is discouraged: while possible, it may leave you in astate that is hard to back out of in the case of a conflict.The third syntax ("git merge --continue") can only be run after the merge has resulted in conflicts.

换乘 rebase 变基更新

git rebase -i master

详细的参数可以通过 --help 参数查看

NAMEgit-rebase - Reapply commits on top of another base tipSYNOPSISgit rebase [-i | --interactive] [<options>] [--exec <cmd>][--onto <newbase> | --keep-base] [<upstream> [<branch>]]git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]--root [<branch>]git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)DESCRIPTIONIf <branch> is specified, git rebase will perform an automatic git switch <branch> before doing anything else. Otherwiseit remains on the current branch.If <upstream> is not specified, the upstream configured in branch.<name>.remote and branch.<name>.merge options will beused (see git-config(1) for details) and the --fork-point option is assumed. If you are currently not on any branch or ifthe current branch does not have a configured upstream, the rebase will abort.All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area. This isthe same set of commits that would be shown by git log <upstream>..HEAD; or by git log 'fork_point'..HEAD, if--fork-point is active (see the description on --fork-point below); or by git log HEAD, if the --root option isspecified.The current branch is reset to <upstream>, or <newbase> if the --onto option was supplied. This has the exact same effectas git reset --hard <upstream> (or <newbase>). ORIG_HEAD is set to point at the tip of the branch before the reset.The commits that were previously saved into the temporary area are then reapplied to the current branch, one by one, inorder. Note that any commits in HEAD which introduce the same textual changes as a commit in HEAD..<upstream> are omitted(i.e., a patch already accepted upstream with a different commit message or timestamp will be skipped).It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolveany such merge failure and run git rebase --continue. Another option is to bypass the commit that caused the mergefailure with git rebase --skip. To check out the original <branch> and remove the .git/rebase-apply working files, usethe command git rebase --abort instead.Assume the following history exists and the current branch is "topic":A---B---C topic/D---E---F---G masterFrom this point, the result of either of the following commands:git rebase mastergit rebase master topicwould be:A'--B'--C' topic/D---E---F---G masterNOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topicwill remain the checked-out branch.

2.2 git 远程仓操作

  • 查看远程仓
    载入远程仓再查看,比如 github
git clone https://github.com/chewitt/RTL8822CS.git
cd RTL8822CS
# 查看远程仓情况
git remote -v
origin  https://github.com/chewitt/RTL8822CS.git (fetch)
origin  https://github.com/chewitt/RTL8822CS.git (push)

或者进入到本地代码仓库,

cd harmony/kernel/linux/linux-5.10/
git remote -v
origin  https://gitee.com/openharmony/kernel_linux_5.10 (fetch)
origin  https://gitee.com/openharmony/kernel_linux_5.10 (push)

- 增加远程仓
git 管理过程难免会远程仓转换,用到命令 git remote建立链接,提交代码即可
命令中 remote_name 远程仓名, remote-url 则是 git 地址

git remote add remote_name remote-url
#如果想提交到远程代码托管
git push remote_name branch

远程仓创建完成,可以再使用 git fetch remote_namegit pull remote_namel完成代码同步。
如果想重新拉取可以尝试如下操作

 git pull --rebase origin master

- 删除远程仓

 git remote prune remote_name#或者git remote remove remote_name

详细的参数可以通过 --help 参数查看

git remote --help
NAMEgit-remote - Manage set of tracked repositoriesSYNOPSISgit remote [-v | --verbose]git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>git remote rename <old> <new>git remote remove <name>git remote set-head <name> (-a | --auto | -d | --delete | <branch>)git remote set-branches [--add] <name> <branch>...git remote get-url [--push] [--all] <name>git remote set-url [--push] <name> <newurl> [<oldurl>]git remote set-url --add [--push] <name> <newurl>git remote set-url --delete [--push] <name> <url>git remote [-v | --verbose] show [-n] <name>...git remote prune [-n | --dry-run] <name>...git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]DESCRIPTIONManage the set of repositories ("remotes") whose branches you track.OPTIONS-v, --verboseBe a little more verbose and show remote url after name. NOTE: This must be placed betweenremote and subcommand.

2.3 patch 应用

  • 生成补丁(patch),或者临时 diff
#生成补丁
git format-patch -n #默认以最新提交为基础, 向后 n 个补丁
git format-patch --HEAD -n # HEAD 提交记录 hash 值为基的向后 n 个补丁

diff 则比较随意

git diff path > xxxx.diff # path 默认为当前目录

-应用打补丁(patch),或临时修改(diff)
打补丁前建先检查再应用,如果检查通过则可以直接应用

git apply --check 00xx-xxxxx.patch
#应用补丁,但不提交
git apply 00xx-xxxxx.patch
#应用补丁和提交记录
git am 00xx-xxxxx.patch

应用临时修改 diff文件

patch -p 01 < xxxx.diff

Tips 如果想要补丁不能直接使用,该如何处理?
答:使用patch 命令直接打补丁,然后再核对修改点。

2.4 git 错误删除后找回提交

参照 1.5 中 git reflog 查看删除的提交记录, 找到 MD5值;根据 MD5 值抽取指定提交点,git cherry-pick MD5 恢复已经删除的提交记录

2.5 git异常锁库

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

简单粗暴解决方法 删掉 index.lock

rm .git/index.lock

Tips

a) 克隆新代码仓,结束后无任何文件并有错误提示。

warning: 远程 HEAD 指向一个不存在的引用,无法检出。

未构建远程 master 分支,检出文件时定位不到代码主分支;解决方法:构建 master 分支,并上传到远程仓。
b) 查看从<commit_id1><commit_id2>之间的所有提交记录

git log <commit_id1>..<commit_id2>

c) 查看某个commit之前的所有提交记录

git log <commit_id>..

d) 查看某个commit之后的所有提交记录

git log ..<commit_id>

结语

好工具提升工作效率。

git 详解-进阶篇相关推荐

  1. Linux使用详解(进阶篇)

    文章目录 Linux使用详解(进阶篇) 1.Linux目录说明 2.操作防火墙 3.ulimit命令和history命令 4.RPM和Yum的使用 5.设置系统字符集 6.vi & vim编辑 ...

  2. Openharmony应用NAPI详解--进阶篇1

    NAPI面向C++的异步接口 3.C++实现NAPI异步接口需要做到三步 同步返回结果给js/ets调用者 另起线程完成异步操作 通过回调(callback)或Promise将异步操作结果返回 4.异 ...

  3. Openharmony应用NAPI详解--进阶篇2

    NAPI面向C++的异步接口(promise) promise方式的处理方式 承接上文,与callback方式不同的是,promise对象由C++侧创建以返回值的方式传递回js/ets侧,promis ...

  4. Openharmony应用NAPI详解--基础篇

    NAPI是什么? 简单点理解就是在Openharmony里,实现上层js或ets应用与底层C/C++之间交互的框架. Openharmony里的官方解释:NAPI(Native API)组件是一套对外 ...

  5. Git详解之二:Git基础

    Git详解之二:Git基础 2012/08/24 | 分类: IT技术, 书籍与教程 | 0 条评论 | 来源: <PRO GIT>     | 标签: GIT, PRO GIT, 版本控 ...

  6. mhdd应用详解-入门篇(图文教程)

    mhdd应用详解-入门篇(图文教程) 来源:wxiu.com 作者:fox 时间:2009-03-13 点击: 54 对于专业的 电脑维修人员来说, MHDD是必备的硬盘工具,但是技术人员一般只拿他 ...

  7. Android-使用Jenkins自动化打包详解-Linux篇

    文章目录 购买和使用阿里云 安装宝塔面板 安装Tomcat 安装 jenkins 配置jenkins 设置FTP jenkins 常规操作 修改admin密码 购买和使用阿里云 首先购买阿里云,可从以 ...

  8. IIS负载均衡-Application Request Route详解第一篇: ARR介绍

    IIS负载均衡-Application Request Route详解第一篇: ARR介绍   说到负载均衡,相信大家已经不再陌生了,本系列主要介绍在IIS中可以采用的负载均衡的软件:微软的Appli ...

  9. 删除隐藏版本信息 版本回退_Git系列之-分布式版本控制Git详解

    课程简介: 课程目标:通过本课程的学习,将全面掌握Git版本管理工具的配置与使用,以适应工作的需要. 适用人群:具有一定开发基础的开发人员. 课程概述:Git (读音为/gɪt/)是一个开源的分布式版 ...

最新文章

  1. MySQL中show语法使用总结
  2. python电影推荐算法_电影推荐系统python实现
  3. Three.js中实现点击按钮添加删除旋转立方体
  4. 零基础学Python(第三章 基础语法)
  5. 基于Schema配置的AOP
  6. Android studio之编译出现 Error:null value in entry: outputDirectory=null
  7. 啊哈C语言,c语言编程入门书籍及PPT(PDF版)下载网址
  8. git已经删除了远程分支,但本地环境仍能看到远程分支
  9. Android判断app是不是第一次启动
  10. Python3+telnetlib实现telnet客户端
  11. perform指标分析_Perform+3D-入门实战指导.ppt
  12. 为什么acdsee服务器怎在运行,为何打开网页时会出现安装acdsee 5.0.1 powerpack
  13. 云计算进入多云时代 青云QingCloud如何帮企业做好多云管理?
  14. 《结对-爬取大麦网演唱会信息-设计文档》
  15. JAVA无法读取jvm.cfg_“could not open `C:\Program Files\Java\jre7\lib\amd64\jvm.cfg”问题解决办法...
  16. S7-200SMART案例分析——运动控制编程
  17. 第五章--第三节:设计复用模式(pattern)
  18. 临界区、互斥量、事件、信号量四种方式
  19. php个人博客项目实训报告,PHP个人博客项目------切切歆语博客
  20. javaweb项目,配置servlet后报错:java.lang.ClassCastException: cannot be cast to class javax.servlet.Servlet

热门文章

  1. NFT Insider #54:BreederDAO将开启公募,游戏巨头万代南梦宫、育碧入局元宇宙
  2. Redis集群的设计与使用
  3. Android层面上对sensor及event事件的处理
  4. [笔记]阿里云物联网之业务服务端(java、php)接入阿里云平台
  5. 百度地图海量点尺寸和大小
  6. buuctf 四面八方(四方密码)
  7. 机器学习入门系列05,Classification: Probabilistic Generative Model(分类:概率生成模型)
  8. XML学习-方立勋视频学习
  9. 【转载】基于多层结构的网络游戏平台的研究与应用
  10. UI界面设计八大原则