git 详解-进阶篇

git 从实践到生产力

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


圈重点 看想学

  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 reflog
  6891e36 HEAD@{0}: reset: moving to 6891e3609ce9dff84ed1233bb21e33b235823684
  ddfe728 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-harmony
  356x-online
  master
# master 分支已经同步到最新代码,当前 356x-harmony 希望合并更新
git merge master

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

git merge --help
NAME
       git-merge - Join two or more development histories together

SYNOPSIS
       git 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)

DESCRIPTION
       Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the
       current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand
       to 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 master

       Then "git merge topic" will replay the changes made on the topic branch since it diverged from master (i.e., E) until its
       current commit (C) on top of master, and record the result in a new commit along with the names of the two parent commits
       and a log message from the user describing the changes.

                     A---B---C topic
                    /         \
               D---E---F---G---H master

       The second syntax ("git merge --abort") can only be run after the merge has resulted in conflicts. git merge --abort will
       abort the merge process and try to reconstruct the pre-merge state. However, if there were uncommitted changes when the
       merge started (and especially if those changes were further modified after the merge was started), git merge --abort will
       in 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 a
       state 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 参数查看

NAME
       git-rebase - Reapply commits on top of another base tip

SYNOPSIS
       git 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)

DESCRIPTION
       If <branch> is specified, git rebase will perform an automatic git switch <branch> before doing anything else. Otherwise
       it remains on the current branch.

       If <upstream> is not specified, the upstream configured in branch.<name>.remote and branch.<name>.merge options will be
       used (see git-config(1) for details) and the --fork-point option is assumed. If you are currently not on any branch or if
       the 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 is
       the 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 is
       specified.

       The current branch is reset to <upstream>, or <newbase> if the --onto option was supplied. This has the exact same effect
       as 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, in
       order. 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 resolve
       any such merge failure and run git rebase --continue. Another option is to bypass the commit that caused the merge
       failure with git rebase --skip. To check out the original <branch> and remove the .git/rebase-apply working files, use
       the command git rebase --abort instead.

       Assume the following history exists and the current branch is "topic":

                     A---B---C topic
                    /
               D---E---F---G master

       From this point, the result of either of the following commands:

           git rebase master
           git rebase master topic

       would be:

                             A'--B'--C' topic
                            /
               D---E---F---G master

       NOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topic
       will 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
NAME
       git-remote - Manage set of tracked repositories

SYNOPSIS
       git 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>)...]

DESCRIPTION
       Manage the set of repositories ("remotes") whose branches you track.

OPTIONS
       -v, --verbose
           Be a little more verbose and show remote url after name. NOTE: This must be placed between
           remote 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>

结语

好工具提升工作效率。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值