Git/分支:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“ == 初始化 == {{shc|<syntaxhighlight lang="bash"> git init</syntaxhighlight>|$|/home/kangkk}} == 命令合并 == 命令可以进行合并。 {{hcdiff|head=|output=<syntaxhighlight lang="bash"> git add /src/a.js git commit -m "edit the file"</syntaxhighlight>|output2=<syntaxhighlight lang="bash"> git commit -a -m "edit the file" </syntaxhighlight>}} == commit == === rebase === === reset === 查看记录 {{shc|<syntaxhighlight lang="bash"> git log --oneline<…”
 
Kangkk​(留言 | 贡献)
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|”
 
(未显示同一用户的16个中间版本)
第1行: 第1行:


== 初始化 ==
{{shc|<syntaxhighlight lang="bash">
git init</syntaxhighlight>|$|/home/kangkk}}
== 命令合并 ==
命令可以进行合并。
{{hcdiff|head=|output=<syntaxhighlight lang="bash">
git add /src/a.js
git commit -m "edit the file"</syntaxhighlight>|output2=<syntaxhighlight lang="bash">
git commit -a -m "edit the file"
</syntaxhighlight>}}
== commit ==
=== rebase ===
=== reset ===
查看记录
{{shc|<syntaxhighlight lang="bash">
git log --oneline</syntaxhighlight>|$|/home/kangkk}}
<div class="pageBoxGreen"><code>^</code> 表示前一次。<code>^^^^</code> 等价于 <code>~4</code>。<br>
{{hcdiff|head=|output=<syntaxhighlight lang="bash">
git reset fb69f9d^^^^</syntaxhighlight>|output2=<syntaxhighlight lang="bash">
git reset fb69f9d~4
</syntaxhighlight>}}</div>
取消最后一次 commit。
{{shc|<syntaxhighlight lang="bash">
git reset fb69f9d^
# or
git reset master^
# or
git reset HEAD^</syntaxhighlight>|$|/home/kangkk}}
回退到指定 commit
{{shc|<syntaxhighlight lang="bash">
git reset fb69f9d</syntaxhighlight>|$|/home/kangkk}}
==== --mixed ====
==== soft ====
==== hard ====
=== --amend ===
=== 追加文件到 commit ===
==== reset ====
删除最后一次 commit,重新 commit。
==== --amend --no-edit ====
将文件并入最后一次 commit。使用 <code>--no-edit</code> 标识不进行编辑 commit 信息。
{{shc|<syntaxhighlight lang="bash">
git add src/a.js && \
git commit --amend --no-edit</syntaxhighlight>|$|/home/kangkk}}
== 析出 ==
=== 析出被删除文件 ===
从暂存区析出文件。
{{shc|<syntaxhighlight lang="bash">
rm src/a.js && \
git checkout src/a.js</syntaxhighlight>|$|/home/kangkk}}
将文件致为上一次 commit 状态。
{{shc|<syntaxhighlight lang="bash">
git checkout HEAD~2 src/a.js</syntaxhighlight>|$|/home/kangkk}}


== 其他 ==
== 其他 ==
=== remote ===
查看远程指引。
{{Shc_out|<syntaxhighlight lang="bash">
git remote
</syntaxhighlight>|<syntaxhighlight lang="bash">old-origin
origin
</syntaxhighlight>|$|/home/kangkk}}
删除远程
{{shc|<syntaxhighlight lang="bash">git remote rm old-origin</syntaxhighlight>|$|/home/kangkk}}
修改远程地址
{{shc|<syntaxhighlight lang="bash">
git remote set-url origin git@w.src.corp.qihoo.net:arena/arena-exp-serve.git</syntaxhighlight>|$|/home/kangkk}}
添加远程地址
{{Shc_out|<syntaxhighlight lang="bash">
git remote add origin git@w.src.corp.qihoo.net:arena/arena-exp-serve.git</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|$|/home/kangkk}}
{{Shc_out|<syntaxhighlight lang="bash">
git remote -v</syntaxhighlight>|<syntaxhighlight lang="bash">
origin  git@v.src.corp.qihoo.net:qiwooiot/font.qiwoo.org.git (fetch)
origin  git@v.src.corp.qihoo.net:qiwooiot/font.qiwoo.org.git (push)
</syntaxhighlight>|$|/home/kangkk}}
=== 常用命令 ===
=== 常用命令 ===
{| class="wikitable"
{| class="wikitable"
第138行: 第51行:
git checkout ejs_vue
git checkout ejs_vue
</pre>
</pre>
== 定时拉取代码 ==
=== crontab ===
{{shc|<syntaxhighlight lang="bash">
crontab -e</syntaxhighlight>|$}}
{{hc|/tmp/crontab.90pWnh|<syntaxhighlight lang="text">
*/1 * * * * cd /root/vue-webpack/;/usr/local/bin/git pull origin ejs_vue
</syntaxhighlight>}}
如果服务器端需要编译,可能会导致冲突
<syntaxhighlight lang="javascript">
0 */1 * * * cd /root/vue-webpack/;/usr/bin/git fetch --all;/usr/bin/git reset --hard origin/ejs_vue;/usr/bin/git pull origin ejs_vue;/usr/local/bin/npm run dev</syntaxhighlight>
=== PHP ===
<syntaxhighlight lang="php">
exec("cd /root/vue-webpack/;/usr/local/bin/git pull origin ejs_vue");</syntaxhighlight>
=== 密码 ===
== 新建分支 ==
== 新建分支 ==
{| class="wikitable"
{| class="wikitable"
第241行: 第138行:
{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
git mv src/a.js src/b.js</syntaxhighlight>|$|/home/kangkk}}
git mv src/a.js src/b.js</syntaxhighlight>|$|/home/kangkk}}
== 远程仓库 ==
=== 临时分支 ===
{| class="wikitable"
|-
| git remote -v || 查询当前远程的版本 || <syntaxhighlight lang="javascript">
origin  git@gitee.com:b***c/ask***-pc.git (fetch)
origin  git@gitee.com:b***c/ask***-pc.git (push)</syntaxhighlight>
|-
| <!-- git pull origin master --> || 直接拉取并合并最新代码( 不推荐 ) || 示例
|-
| git fetch origin master:mastertmp || 获取最新代码到本地临时分支 || 示例
|-
| git diff mastertmp || 查看本地 mastertmp 分支与当前分支的版本差异 ||
|-
| git merge mastertmp || 合并最新临时分支到本地分支 ||
|-
| git branch -D mastertmp || 删除本地临时分支 ||
|}
=== 没有临时分支 ===
{| class="wikitable"
|-
| git remote -v || 查询当前远程的版本 || <syntaxhighlight lang="javascript">
origin  git@gitee.com:b***c/ask***-pc.git (fetch)
origin  git@gitee.com:b***c/ask***-pc.git (push)</syntaxhighlight>
|-
| git fetch origin master || 获取远端的 origin/master 分支 || 示例
|-
| git log -p master..origin/master ||查看本地 master 与远端 origin/master 的版本差异 || 示例
|-
| git merge origin/master || 合并远端分支 origin/master 到当前分支 ||
|}


== 重置本地分支 ==
== 重置本地分支 ==
第306行: 第171行:
无文件要提交,干净的工作区
无文件要提交,干净的工作区
</syntaxhighlight>|$|/home/kangkk}}
</syntaxhighlight>|$|/home/kangkk}}
== 提交 ==
=== add ===
{| class="wikitable"
|-
! 参数!! 描述 !! 示例
|-
| . || 当前目录全部,递归子目录 || git add .
|-
| --all || 当前项目全部,递归子目录 || git add --all
|-
| 文件相对地址 || 部分 || git add src/a.js
|}
=== commit ===


== git pull ==
== git pull ==
第336行: 第186行:


<code>git pull</code> 出现冲突后可以暂存本地修改 <code>git stash</code>,然后 <code>git pull</code> 更新代码,<code>git stash list</code> 可查看暂存记录列表,释放本地暂存 <code>git stash apply stash@{0}</code>。
<code>git pull</code> 出现冲突后可以暂存本地修改 <code>git stash</code>,然后 <code>git pull</code> 更新代码,<code>git stash list</code> 可查看暂存记录列表,释放本地暂存 <code>git stash apply stash@{0}</code>。
== diff ==
任意两个版本的差异
{{shc|<syntaxhighlight lang="bash">
git diff master..test</syntaxhighlight>|$}}
master , test 的共有父分支和 test 分支之间的差异
{{shc|<syntaxhighlight lang="bash">
git diff master...test</syntaxhighlight>|$}}
当前工作目录和上次提交与本地索引间的差异
{{shc|<syntaxhighlight lang="bash">
git diff</syntaxhighlight>|$}}
查看在下次提交时要提交的内容,显示当前的索引和上次提交间的差异
{{shc|<syntaxhighlight lang="bash">
git diff --cached</syntaxhighlight>|$}}
显示工作目录与上次提交时之间的所有差别
{{shc|<syntaxhighlight lang="bash">
git diff HEAD</syntaxhighlight>|$}}
== commit ==
{{Shc_out|<syntaxhighlight lang="bash">
git log --stat</syntaxhighlight>|<syntaxhighlight lang="bash">commit 1c791646bf9605c4380f94a84f3a89b8cc2fb60c (HEAD -> dev)
Merge: 823544a 2b2fa58
Author: kangjian <kangjian@360.cn>
Date:  Tue Jul 13 11:41:29 2021 +0800
    修改登录页大小
commit 823544a7986d8e2913d443af0a58290b3fbc1f50
Author: kangjian <kangjian@360.cn>
Date:  Tue Jul 13 11:36:16 2021 +0800
    修改登录页大小
ph-test/.gitignore                                                                                  |  6 +++---
ph-test/plugin/sign/net.qihoo.design/META-INF/signatures.xml                                        |  10 +++++-----
...ndex_page_script.66bceac724c9b0717f19.min.css => index_page_script.3898d67102797bd90e12.min.css} |  2 +-
.../css/{main_style.66bceac724c9b0717f19.min.css => main_style.3898d67102797bd90e12.min.css}        |  0
ph-test/plugin/sign/net.qihoo.design/index.html                                                    |  2 +-
.../net.qihoo.design/js/{index_page_script.66bceac7.min.js => index_page_script.3898d671.min.js}    |  0
.../js/{index_page_script.66bceac7.min.js.gz => index_page_script.3898d671.min.js.gz}              | Bin
ph-test/plugin/sign/net.qihoo.design/manifest.json                                                  |  8 ++++----
ph-test/view/page/login/Frame.vue                                                                  |  11 +++++++----
9 files changed, 21 insertions(+), 18 deletions(-)
</syntaxhighlight>|$|/home/kangkk}}
=== 参考 ===
* https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2
== 其他 ==
{| class="wikitable"
|-
| [[/清理仓库|清理仓库]]||[[/gitlab|gitlab]]
|-
| [[/ssh|ssh]]|| [[/environment_variable|environment variable]]
|}
== 回滚 ==
{{Shc_out|<syntaxhighlight lang="bash">
git log
</syntaxhighlight>|<syntaxhighlight lang="bash">commit 71d6d4d66ad34d815372dde78a9afc64397ad3ee (HEAD -> test, origin/test)
Author: kangkk <365@kangkk.com>
Date:  Tue Mar 9 10:41:05 2021 +0800
    添加package-lock
commit 90cf19e637d4852262e5925094f3c4cae40f10ef
Merge: 4930a02 edc565d
Author: kangkk <365@kangkk.com>
Date:  Tue Mar 9 10:31:52 2021 +0800
    修复冲突
commit edc565d95099e428005a415524b6735405efba4e (origin/fix-coverIsNull, fix-coverIsNull)
Author: kangkk <365@kangkk.com>
Date:  Tue Mar 9 10:28:43 2021 +0800
    修复封面历史问题
commit aba9aaa7c550eb78141c79b618096747be141ca0
Author: kangkk <365@kangkk.com>
Date:  Tue Mar 9 10:13:55 2021 +0800
    修复封面历史问题
commit 988fb5dd4719e82c951b4cbfbe6b2d784290e1eb
Author: kangkk <365@kangkk.com>
Date:  Mon Mar 8 18:57:22 2021 +0800
</syntaxhighlight>|$|/home/kangkk}}
然后回滚分支
{{shc|<syntaxhighlight lang="bash">git reset --hard 988fb5dd4719e82c951b4cbfbe6b2d784290e1eb
</syntaxhighlight>|$|/home/kangkk}}
查看修改状态
{{shc|<syntaxhighlight lang="bash">git status
</syntaxhighlight>|$|/home/kangkk}}
远程仓库删除原有分支
{{shc|<syntaxhighlight lang="bash">git push origin :test
git push origin test
</syntaxhighlight>|$|/home/kangkk}}


== 分支之间差异 ==
== 分支之间差异 ==
第456行: 第205行:
git log --left-right dev...master</syntaxhighlight>|$}}
git log --left-right dev...master</syntaxhighlight>|$}}


{{leftrightflex|
{{Double-columns|
注意 commit 后面的箭头,根据我们在 –left-right dev…master 的顺序,左箭头 < 表示是 dev 的,右箭头 > 表示是 master的}}
注意 commit 后面的箭头,根据我们在 –left-right dev…master 的顺序,左箭头 < 表示是 dev 的,右箭头 > 表示是 master的}}
{{Unknown}}
{{Unknown}}
第498行: 第247行:
== cleanup ==
== cleanup ==
所有被 <code>ignore</code>(忽略)的本地文件会被全部删除,而且是无法恢复的,因为远程仓库里根本就没有这些文件。
所有被 <code>ignore</code>(忽略)的本地文件会被全部删除,而且是无法恢复的,因为远程仓库里根本就没有这些文件。
== gitee ==
多账号
<syntaxhighlight lang="javascript">
git clone git@gitee.com:bjz***/askgo-admin-fe.git</syntaxhighlight>
== .gitignore ==
== .gitignore ==
[[/gitignore|.gitignore]]
[[/gitignore|.gitignore]]
第513行: 第258行:
git push -u origin kk_test
git push -u origin kk_test
</syntaxhighlight>|#}}
</syntaxhighlight>|#}}
== 迁移 ==
查看当前仓库地址。
{{shc|<syntaxhighlight lang="bash">
git remote -v</syntaxhighlight>|$|/home/kangkk}}
=== git push --mirror ===
=== git remote set-url ===
迁移单个分支。
{{Shc_out|<syntaxhighlight lang="bash">
git remote set-url origin ssh://git@192.168.31.5:10022/abc/xyz.git</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|$|/home/kangkk}}
{{shc|<syntaxhighlight lang="bash">
git push</syntaxhighlight>|$|/home/kangkk}}
迁移所有分支。
{{Shc_out|<syntaxhighlight lang="bash">
cd existing_repo && \
git remote rename origin old-origin && \
git remote add origin ssh://git@192.168.31.5:10022/qihoo/medici-doc.git && \
git push -u origin --all && \
git push -u origin --tags</syntaxhighlight>|<syntaxhighlight lang="bash">
枚举对象中: 81, 完成.
对象计数中: 100% (81/81), 完成.
使用 12 个线程进行压缩
压缩对象中: 100% (59/59), 完成.
写入对象中: 100% (81/81), 336.01 KiB | 84.00 MiB/s, 完成.
总共 81(差异 15),复用 81(差异 15),包复用 0
To ssh://192.168.31.5:10022/abc/doc.git
* [new branch]      master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。
</syntaxhighlight>|$|/home/kangkk}}
检查仓库地址更改状态。
{{Shc_out|<syntaxhighlight lang="bash">
git remote show origin</syntaxhighlight>|<syntaxhighlight lang="bash">
* 远程 origin
  获取地址:ssh://git@192.168.31.5:10022/abc/xyz.git
  推送地址:ssh://git@192.168.31.5:10022/abc/xyz.git
  HEAD 分支:main
  远程分支:
    main                      新的(下一次获取将存储于 remotes/origin)
    refs/remotes/origin/master 过时(使用 'git remote prune' 来移除)
  为 'git pull' 配置的本地分支:
    master 与远程 master 合并
</syntaxhighlight>|$|/home/kangkk}}