当前位置:   article > 正文

git 详解-进阶篇_git 进阶

git 进阶

git 从实践到生产力

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


圈重点 看想学

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

1. Git 分支管理

1.1 git 分支管理常用命令

① 查看项目的分支

  git branch #本地
  git branch -r #远程
  git branch -a #本地和远程
  • 1
  • 2
  • 3

切换分支

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

③ 重命名git本地分支

git branch -m old_local_branch_name new_local_branch_name
  • 1

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

 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 #提交新分支
  • 1
  • 2
  • 3

删除本地分支

 git branch -d <BranchName>
  • 1

⑥ 删除远程分支

  git push origin <BranchName> --delete
  • 1

⑦ 推送本地分支

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

⑧ 提交覆盖更新远程分支

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

⑨ 删除本地跟踪 remote

 git branch -rd origin/branch
  • 1

1.2 提交记录,代码回滚

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

git log --pretty=oneline path/fileName
  • 1

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

git log path/fileName
  • 1

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

git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path/fileName
  • 1
  • 2

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

git reset commit-id --hard
  • 1

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

git reset commit-id
git reset commit-id --soft 
  • 1
  • 2

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

git revert commit-id
  • 1

b) 回滚多个提交

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

或者变基更新,生成新 commit

git rebase -i commit-id
  • 1

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

git reset --hard <commit-id>
  • 1

② 强制覆盖提交

 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 未跟踪文件添加删除
    显示未跟踪文件
git ls-files --others --exclude-standard
  • 1

添加未追踪文件

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

1.4 git 代码提交/拉取

a) 提交代码到本地

git commit path/file -m "commit log"
  • 1

推送到远程

git push remote_name local_branch:remoteb_branch
  • 1

b) 拉取代码到本地

git fetch remote_name
git pull remote_name local_branch:remoteb_branch
  • 1
  • 2

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

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

 git reflog
  6891e36 HEAD@{0}: reset: moving to 6891e3609ce9dff84ed1233bb21e33b235823684
  ddfe728 HEAD@{1}: checkout: moving from hra to t6s
  • 1
  • 2
  • 3

b) 恢复指定删除提交

git cherry-pick 6891e36
  • 1

1.6 tag(标签)操作

a) 显示本地tag

git tag
  • 1

b) tag 增加

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

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

git tag -d tagName
  • 1

Step2: 删除远程tag

git push origin branch :refs/tags/tagName
  • 1

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 #简单粗暴处理
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. git 分支管理高阶操作

2.1 分支合并

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

git branch
* 356x-harmony
  356x-online
  master
# master 分支已经同步到最新代码,当前 356x-harmony 希望合并更新
git merge master
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

详细的参数可以通过 --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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

换乘 rebase 变基更新

git rebase -i master
  • 1

详细的参数可以通过 --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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

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)
  • 1
  • 2
  • 3
  • 4

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

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

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

 git pull --rebase origin master
  • 1

- 删除远程仓

 git remote prune remote_name
 #或者
 git remote remove remote_name
  • 1
  • 2
  • 3

详细的参数可以通过 --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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2.3 patch 应用

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

diff 则比较随意

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

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

git apply --check 00xx-xxxxx.patch
#应用补丁,但不提交
git apply 00xx-xxxxx.patch
#应用补丁和提交记录
git am 00xx-xxxxx.patch
  • 1
  • 2
  • 3
  • 4
  • 5

应用临时修改 diff文件

patch -p 01 < xxxx.diff
  • 1

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.
  • 1
  • 2
  • 3
  • 4
  • 5

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

rm .git/index.lock
  • 1

Tips

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

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

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

git log <commit_id1>..<commit_id2>
  • 1

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

git log <commit_id>..
  • 1

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

git log ..<commit_id>
  • 1

结语

好工具提升工作效率。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/589118
推荐阅读
相关标签
  

闽ICP备14008679号