2.5 几个设置示例
2.5.1 变基(rebase)与归并(merge)设置
默认情况下,在实行 git pull 时,如果本地分支的汗青记录与远程分支的汗青差别,则会举行归并提交。修改默认方式可使用:
- # use rebase instead of merge: true / false
- $ git config pull.rebase true
复制代码 切换分支时,取消默认按归并(merge)的方式举行分支切换:
- # never(default) lacal remote always
- $ git config branch.autosetuprebase always
复制代码 设置详细某分支在实行 git pull 操作时按 git rebase 举行:
- # pattern: branch.<name>.rebase approach
- $ git config branch.feature/2.rebase true
复制代码 2.5.2 有效期设置
默认情况下,Git 将对未引用对象实行垃圾回收,并对为超过 90 天的 reflog 条目举行清算。一个 git 对象必须被其他对象引用,这个其他对象可以是一个 tree 树,一个 tag 标签,一个 branch 分支,大概其他 Git 内部簿记,如 stash 或 reflog。有三个设置项可对有效期举行设置:
- gc.reflogexpire
- gc.reflogexpireunreachable
- gc.pruneexpire
- # To set a non-default expiry date on remote branches only
- $ git config gc./refs/remote/*.reflogexpire never
- # How long the reflog entries that are not a part of the current branch history should be available in the repository, 30d by default:
- $ git config gc./refs/remote/*.reflogexpireunreachable "2 months"
- # Set a date so git gc will prune objects sooner
- $ git config gc.pruneexpire 3.days.ago
复制代码 2.5.3 设置自动更正
出现笔误时 git 会自动提示:
可以设置自动更正的时间隔断,单元 厘秒(0.1秒):
- $ git config help.autocorrect 5
- $ git statis
- WARNING: You called a Git command named 'statis', which does not exist.
- Continuing under the assumption that you meant 'status'
- in 0.5 seconds automatically...
- # On branch master
- # Changes to be committed:
- # (use "git reset HEAD <file>..." to unstage)
- #
- # modified: another-file.txt
- #
复制代码 实测效果:(设为 3 秒后自动更正)
2.6 设置 Git 别名
设置常见通用别名:
- $ git config --global alias.co checkout
- $ git config --global alias.br branch
- $ git config --global alias.ci commit
- $ git config --global alias.st status
复制代码 新增自定义别名:让某文件取消暂存
- # pattern: git unstage <FILE>...
- $ git config --global alias.unstage 'reset HEAD --'
- # Test
- $ echo "Something new" >> .\README.md
- $ git unstage .\README.md
- Unstaged changes after reset:
- M README.md
- $ git unstage .\README.md
- Unstaged changes after reset:
- M README.md
- $ git st
- On branch master
- Your branch is behind 'origin/master' by 5729 commits, and can be fast-forwarded.
- (use "git pull" to update your local branch)
- Changes not staged for commit:
- (use "git add <file>..." to update what will be committed)
- (use "git restore <file>..." to discard changes in working directory)
- modified: README.md
- no changes added to commit (use "git add" and/or "git commit -a")
复制代码 实测截图:
更常见的是将自定义的 git 格式化后的日志形式存为一个别名:
- $ git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Cred%d %Creset%s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --numstat"
复制代码 实测效果:
git 也可以为自定义的外部脚本设置别名,此时别名内容前必要加感叹号 !。示例如下:
- # add the line onto your ~/.gitconfig file
- editconflicted = "!f() {git ls-files --unmerged | cut -f2 | sort -u ; }; $EDITOR 'f'"
复制代码 这段脚本会打开预设的编辑器,此中包罗由于归并或变基而处于辩说状态的全部文件。 该操作可以快速修复辩说并继续归并/变基。
测试如下:
- $ git branch A 03f78fc
- $ git branch B 9891497
- $ git checkout A
- Switched to branch 'A'
- $ git merge B
- # conflicts found, and a list of conlicted files print out
- # Resolve all these files and then run the script
- $ git editconflicted
- # Check the status
- $ git st
- # then commit to resolve merge
- $ git ci
复制代码 另一种创建 Git 别名的方法,是新建一个 shell 脚本,并使用名称 git-<your-alias-name> 生存。 设置文件权限为 可实行,并将其放在 $PATH 某一路径下,用命令行运行 git <your-alias-name> 看到相应结果。
查看当前定义的全部别名:
- # Linux
- $ git config --list | grep alias
- # Powershell
- > git config --list | where {$_ -match "alias"}
复制代码 2.7 refspec 举例
设置 git 时,很难第一时间就想到 refspec,由于它通常是在实行 git 命令时被隐式调用。当实行堆栈复制或添加远程分支时,.git/config 文件会有类似设置:
- [remote "origin"]
- url = https://git.eclipse.org/r/jgit/jgit
- fetch = +refs/heads/*:refs/remotes/origin/*
复制代码 此中 fetch 这行就包罗了该堆栈实行 fetch 时的 refspec。
本例将在本地创建一个 bare 堆栈(工作目录为空的堆栈),以便示例代码实行 push 操作(否则会覆盖目标堆栈的工作区(work area)和索引(index)。
- # Create bare repo from Eclipse remote repo
- $ git clone --bare https://git.eclipse.org/r/jgit/jgit jgit-bare.git
- # Init local repo for demo
- $ git init refspec-tests
- Initialized empty Git repository in /Users/john.doe/refspec-tests/.git/
- # Add remote origin for the local repo
- $ cd refspec-tests
- $ git remote add origin ../jgit-bare.git
- # Enter the bare repo and rename "stable-*" branches into "stable/*"
- $ cd ../jgit-bare.git && for br in $(git branch -a | grep "stable-"); do new=$(echo $br| sed 's/-/\//'); git branch $new $br; done
复制代码 这里发现两处笔误:
- Linux 情况下实行 sed 命令,更换为目标字符串的 / 必要转义,写作 \/,书中写为 /;
- 远程分支的重命名,应该在 bare 堆栈中举行,原书中少了一句 cd ../jgit-bare.git。反馈给作者核实后,两处笔误均已更正。
- 实测时由于与作者成书时的隔断已有三年多之久,这其间 jgit 库一直在更新;加上国内网络情况查,无法按照书中的命令在线同步 jgit 代码库。变通办法为:先同步 jgit 远程库到本地,然后重命名 stable 分支,再以此为根本创建 bare 堆栈。如果本地库不预先拉取 jgit 的全部远程分支,拷贝 bare 库时将丢失全部远程分支。切记!
关于离线版的预备工作,操作如下:
- # ... get local copy of jgit repo, named demo ... (by fortune)
- # Rename branches: e.g. stable-1.2 --> stable/1.2
- $ cd ./demo && for br in $(git branch -a | grep "stable-"); do new=$(echo $br | sed 's/-/\//'); git branch $new $br; done
- # clone bare repo based on local 'demo' repo
- $ cd .. && git clone --bare ./demo jgit-bare.git
- # ... (same as mentioned above)
复制代码 再次强调
再次强调
再次强调
从本地堆栈新建 bare 库时,数据源堆栈的 全部本地分支 将作为 bare 库的远程跟踪分支(remote-tracking branch),被后续基于该 bare 库的其他衍生堆栈查询出来。因此第 2 步中必须先将示例要用到的全部门支(即全部带 stable- 前缀的分支)签出。
示例一:只同步拉取 master 分支
- # Edit .git/config file: heads/* --> heads/master; origin/* --> origin/master
- [remote "origin"]
- url = ../jgit-bare.git
- fetch = +refs/heads/master:refs/remotes/origin/master
复制代码 设置生效后,实行如下命令将只对 master 分支生效:
- git fetch
- git pull
- git remote update origin
示例二:只同步带 stable 字样的分支
- [remote "origin"]
- url = ../jgit-bare.git
- fetch = +refs/heads/master:refs/remotes/origin/master
- fetch = +refs/heads/stable/*:refs/remotes/origin/stable/*
复制代码 示例三:设置推送到远程堆栈的默认远程分支名称
添加默认推送名称:
- [remote "origin"]
- url = ../jgit-bare.git
- fetch = +refs/heads/master:refs/remotes/origin/master
- fetch = +refs/heads/stable/*:refs/remotes/origin/stable/*
- push = refs/heads/develop:refs/remotes/origin/integration/master
复制代码 创建本地分支并提交一个新版本:
- # Create local branch 'develop', then add one commit
- $ git checkout -b develop
- Switched to a new branch 'develop'
- $ echo "This is the developer setup, read carefully" > readme-dev.txt
- $ git add readme-dev.txt
- $ git commit -m "adds readme file for developers"
复制代码 推送到远程分支(确认是否默认推送至 integration/master 分支):
实测结果:
几点说明:
- integration/master 分支仅在本地存在,远程堆栈(bare 库)不存在。
- refspec 的格式为:<source>:<destination>:
- 对 fetch 操作:远程库为 source,本地库为 destination;
- 对 push 操作:远程库为 destination,本地库为 source;
- refspec 中的 + 号,是指详细的引用写法可以被修改,即便不是快进式修改(即按时间顺序举行的修改,原文为:… to indicate that the ref pattern can be updated even though it isn’t a fast-forward update)
- refspec 不支持模糊匹配,因此不能写作:
- fetch = +refs/heads/stable*:refs/remotes/origin/stable*
复制代码 但 ref 的写法可以按命名空间设置,如:
- fetch = +refs/heads/stable/*:refs/remotes/origin/stable/*
复制代码 这也是为什么示例一开始就要重命名分支名称的根本缘故原由;同时也从另一个角度说明,分支的命名最好按命名空间的方式举行设计。
关于上面提到的笔误,当时还联系了出书社求证,并有幸得到了作者的反馈,确实是写错了。原文如下:
Hi Anton,
Hope you are doing well.
This is what we have received from the author for the first query:
"I have walked through the section, and the missing \ in the command is correct.
Moreover, in order to archive what is described in the section, it is vital to mention that the renaming command (for br …) must be executed in the directory jgit-bare.git. If you follow the commands in the section strictly, you will be in the directory refspec-tests.
By changing the “for br …” command to “(cd …/jgit-bare.git && for br …)”, it will work. The parenthesis at the beginning and at the end are important."
(第二章完)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |