Git:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
无编辑摘要
Kangkk​(留言 | 贡献)
无编辑摘要
第570行: 第570行:
     master 与远程 master 合并
     master 与远程 master 合并
</syntaxhighlight>|$|/home/kangkk}}
</syntaxhighlight>|$|/home/kangkk}}
== 工具 ==
=== git_stats ===
=== cloc ===
{{shc|<syntaxhighlight lang="bash">
brew install cloc
</syntaxhighlight>|jian:kang kangjian$}}
使用。
{{Shc_out|<syntaxhighlight lang="bash">
cloc ./</syntaxhighlight>|<syntaxhighlight lang="bash">
    378 text files.
    345 unique files.
      50 files ignored.
github.com/AlDanial/cloc v 1.92  T=0.62 s (555.8 files/s, 110143.4 lines/s)
-------------------------------------------------------------------------------
Language                    files          blank        comment          code
-------------------------------------------------------------------------------
JSON                            11              2              0          37638
JavaScript                    190          1697          2924          16433
CSS                            31            247            106          3148
LESS                            42            164            139          2875
SVG                            31              7              9          1059
TypeScript                      24            121            186            850
Markdown                        7            72              0            243
YAML                            1            22            31            160
HTML                            2              2            11            124
XML                              1              0              0            42
Dockerfile                      5            13              0            39
-------------------------------------------------------------------------------
SUM:                          345          2347          3406          62611
-------------------------------------------------------------------------------
</syntaxhighlight>|$|/home/kangkk}}
== 其他 ==
=== git-lfs ===
使用 [https://github.com/git-lfs/git-lfs git-lfs] 管理大文件。
== error ==
* Automatic merge failed; fix conflicts and then commit the result.
: {{shc_out|<syntaxhighlight lang="bash">
git status</syntaxhighlight>|<syntaxhighlight lang="text">
On branch newsearch
Your branch is up to date with 'origin/newsearch'.
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
Changes to be committed:
        new file:  .babelrc
        modified:  .eslintignore
        modified:  api/consult.js
        modified:  api/prepose.js
        new file:  assets/img/search/collect-2.png
...
Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:  README.md
        both modified:  api/conf.js
        both added:      api/search.js
...
</syntaxhighlight>|$}}
:: 1. 丢弃本地分支内容,本地的内容将会被丢弃,慎用
::{{shc|<syntaxhighlight lang="bash">
git reset --hard origin/mergesearch</syntaxhighlight>|$}}
:: 或者
::{{shc|<syntaxhighlight lang="bash">
git reset --hard ORIG_HEAD # 慎用</syntaxhighlight>|$}}
:: 2. 合并以后的文件内容混乱,废弃此次合并,回到合并之前的状态
:: {{shc|<syntaxhighlight lang="bash">
git reset --hard HEAD</syntaxhighlight>|$}}
* error: Merging is not possible because you have unmerged files.
: {{shc|<syntaxhighlight lang="bash">
git diff
git status</syntaxhighlight>|$}}
* fatal: cannot do a partial commit during a merge.
: git 认为有部分代码没有做好提交的准备
: {{shc_out|<syntaxhighlight lang="bash">
git commit -a</syntaxhighlight>|<syntaxhighlight lang="text">
  GNU nano 2.9.3                                      /mnt/d/gitee/***-**-pc/.git/COMMIT_EDITMSG                                               
Merge branch 'newsearch' of gitee.com:bjzyxc/ask-go-pc into newsearch
# Conflicts:
#      README.md
#      api/conf.js
#      api/search.js
#      components/common/header/index.vue
#      config/env.js
#      layouts/search.vue
#      pages/search.vue
#      pages/search/list.vue
#      pages/search/listLeftArticle.vue
#      pages/search/listLeftUser.vue
#      pages/search/listRight.vue
#      pages/search/navmenu.vue
#      plugins/bus.js
#      plugins/router.js
#      server/index.js
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#      .git/MERGE_HEAD
# and try again.
                                                                [ Read 115 lines ]
^G Get Help    ^O Write Out    ^W Where Is    ^K Cut Text    ^J Justify      ^C Cur Pos      M-U Undo        M-A Mark Text  M-] To Bracket
^X Exit        ^R Read File    ^\ Replace      ^U Uncut Text  ^T To Spell    ^_ Go To Line  M-E Redo        M-6 Copy Text  M-W WhereIs Next</syntaxhighlight>|$}}
*  It looks like you may be committing a merge. # If this is not correct, please remove the file
* <syntaxhighlight lang="javascript">
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From w.src.corp.qihoo.net:design/dome
  e99cf03..86a552c  master    -> origin/master
error: cannot lock ref 'refs/remotes/origin/test': Unable to create '/mnt/d/360/demo/.git/refs/remotes/origin/test.lock': File exists.
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.
! f15b60c..0bacea2  test      -> origin/test  (unable to update local ref)</syntaxhighlight>
* <code>! [rejected] fixWebLabel -> fixWebLabel (non-fast-forward)</code>
:: {{shc|<syntaxhighlight lang="bash">
git pull --rebase origin fixWebLabel
git push -u origin fixWebLabel</syntaxhighlight>|$|/home/kangkk}}
* error: pathspec 'yy-dev' did not match any file(s) known to git
: 先检查分支 <code>git branch -a</code>
* 未完成合并(MERGE_HEAD存在)
:: {{shc|<syntaxhighlight lang="bash">
git merge --abort
git reset --merge
git pull</syntaxhighlight>|$|/home/kangkk}}

2024年2月29日 (四) 11:07的版本


初始化

/home/kangkk
git init

命令合并

命令可以进行合并。


git add /src/a.js
git commit -m "edit the file"
git commit -a -m "edit the file"

commit

rebase

reset

查看记录

/home/kangkk
git log --oneline
^ 表示前一次。^^^^ 等价于 ~4。

git reset fb69f9d^^^^
git reset fb69f9d~4

取消最后一次 commit。

/home/kangkk
git reset fb69f9d^
# or
git reset master^
# or
git reset HEAD^

回退到指定 commit

/home/kangkk
git reset fb69f9d

--mixed

soft

hard

--amend

追加文件到 commit

reset

删除最后一次 commit,重新 commit。

--amend --no-edit

将文件并入最后一次 commit。使用 --no-edit 标识不进行编辑 commit 信息。

/home/kangkk
git add src/a.js && \
git commit --amend --no-edit

析出

析出被删除文件

从暂存区析出文件。

/home/kangkk
rm src/a.js && \
git checkout src/a.js

将文件致为上一次 commit 状态。

/home/kangkk
git checkout HEAD~2 src/a.js

其他

remote

查看远程指引。

[jian@kang /home/kangkk]$
git remote
old-origin
origin

删除远程

/home/kangkk
git remote rm old-origin

修改远程地址

/home/kangkk
git remote set-url origin git@w.src.corp.qihoo.net:arena/arena-exp-serve.git

添加远程地址

[jian@kang /home/kangkk]$
git remote add origin git@w.src.corp.qihoo.net:arena/arena-exp-serve.git
[jian@kang /home/kangkk]$
git remote -v
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)

常用命令

从master分支上切出一个test分支
 
git checkout master   //切换到本地master分支
git pull origin master   //更新远程master分支到本地
git checkout -b newBranch(本地)  //创建并切换到newBranch分支
git push -u origin newBranch(远程) //推送到远程并与远程的newBranch关联
合并本地分支
 
git checkout new_branch
git merge old_branch
old_branch 合并到 new_branch
合并本地分支
 
git checkout new_branch
git rebase old_branch
old_branch 更新到 new_branch
本地分支对比远程差异 git diff --stat kk origin/kk 示例
示例 git log kk..origin/kk 示例
拉去远程分支 git clone -b kk git@abc.com:user/project.git 示例
本地分支回滚 git reset --hard commit-id 回滚到commit-id
git reset --hard HEAD~3 将最近3次的提交回滚
远程分支回滚
 
git checkout the_branch
git pull
git branch the_branch_backup        # 备份一下这个分支当前的情况
git reset --hard the_commit_id      # the_branch 本地回滚到 the_commit_id
git push origin :the_branch         # 删除远程 the_branch
git push origin the_branch          # 用回滚后的本地分支重新建立远程分支
git push origin :the_branch_backup  # 如果前面都成功了,删除这个备份分支
回滚到commit-id
 
git reset --hard HEAD~1             # 本地代码回滚到上一版本(或者指定版本)
git push -f origin master           # 加入-f参数,强制提交,远程端将强制跟新到reset版本
将最近3次的提交回滚

切换分支

git clone https://github.com/kangkk/vue-webpack.git
cd vue-webpack/
git branch -a
git branch
git checkout ejs_vue

定时拉取代码

crontab

 
crontab -e
/tmp/crontab.90pWnh
*/1 * * * * cd /root/vue-webpack/;/usr/local/bin/git pull origin ejs_vue

如果服务器端需要编译,可能会导致冲突

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

PHP

exec("cd /root/vue-webpack/;/usr/local/bin/git pull origin ejs_vue");

密码

新建分支

切换到基础分支
git checkout master
创建并切换到新分支
git checkout -b hbs
更新分支代码并提交
git add .
# git add *
git commit -m "init"
# git commit -a -m 'init'
推送远端
git push origin hbs

分支创建分支

 
git checkout mergesearch
git checkout -b newpages
git push origin newpages
git pull
# git branch --set-upstream-to=origin/newpages

合并

merge

切换基础分支 git checkout master
合并 git merge hbs
分支合并分支

rebase

撤销

撤销 add

查看 git 状态

[jian@kang /home/kangkk]$
git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .eslintrc
        .gitignore
        README.md
        development.js
        doc/
        nginx.conf
        package-lock.json
        package.json
        pm2.json
        production.js
        src/
        test/
        view/
        www/

nothing added to commit but untracked files present (use "git add" to track)

删除

删除分支

删除本地分支

切换基础分支 git checkout master
删除 branch -d

删除远程分支

删除文件

/home/kangkk
git rm src/a.js

--cached

让文件脱离 git 控制,但不真删除文件。

/home/kangkk
git rm src/a.js --cached

移动

/home/kangkk
git mv src/a.js src/b.js

远程仓库

临时分支

git remote -v 查询当前远程的版本
origin  git@gitee.com:b***c/ask***-pc.git (fetch)
origin  git@gitee.com:b***c/ask***-pc.git (push)
直接拉取并合并最新代码( 不推荐 ) 示例
git fetch origin master:mastertmp 获取最新代码到本地临时分支 示例
git diff mastertmp 查看本地 mastertmp 分支与当前分支的版本差异
git merge mastertmp 合并最新临时分支到本地分支
git branch -D mastertmp 删除本地临时分支

没有临时分支

git remote -v 查询当前远程的版本
origin  git@gitee.com:b***c/ask***-pc.git (fetch)
origin  git@gitee.com:b***c/ask***-pc.git (push)
git fetch origin master 获取远端的 origin/master 分支 示例
git log -p master..origin/master 查看本地 master 与远端 origin/master 的版本差异 示例
git merge origin/master 合并远端分支 origin/master 到当前分支

重置本地分支

拉去远程分支(可选)
git fetch --all
git fetch <远程主机名> <分支名>
丢弃本地变更重置为远端分支内容
 git reset --hard origin/branchName
git pull
git pull <远程主机名> <远程分支名>:<本地分支名>

拉取

从远程拉取分支到本地

 
git checkout -b 本地分支名 origin/远程分支名

提交本地分支作为远程分支

 
git push origin 本地分支名:远程分支名

状态

[jian@kang /home/kangkk]$
git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。

无文件要提交,干净的工作区

提交

add

参数 描述 示例
. 当前目录全部,递归子目录 git add .
--all 当前项目全部,递归子目录 git add --all
文件相对地址 部分 git add src/a.js

commit

git pull

冲突

忽略本地修改,强制拉取远程到本地

git fetch --all
git reset --hard origin/dev
git pull

stash

git stash

git pull 出现冲突后可以暂存本地修改 git stash,然后 git pull 更新代码,git stash list 可查看暂存记录列表,释放本地暂存 git stash apply stash@{0}。

diff

任意两个版本的差异

 
git diff master..test

master , test 的共有父分支和 test 分支之间的差异

 
git diff master...test

当前工作目录和上次提交与本地索引间的差异

 
git diff

查看在下次提交时要提交的内容,显示当前的索引和上次提交间的差异

 
git diff --cached

显示工作目录与上次提交时之间的所有差别

 
git diff HEAD

commit

[jian@kang /home/kangkk]$
git log --stat
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(-)

参考

其他

清理仓库 gitlab
ssh environment variable

回滚

[jian@kang /home/kangkk]$
git log
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

然后回滚分支

/home/kangkk
git reset --hard 988fb5dd4719e82c951b4cbfbe6b2d784290e1eb

查看修改状态

/home/kangkk
git status

远程仓库删除原有分支

/home/kangkk
git push origin :test
git push origin test

分支之间差异

^

查看 dev 有,而 master 中没有

 
git log dev ^master

查看 dev 中比 master 中多提交了哪些内容

 
git log master..dev

不知道谁提交的多谁提交的少,单纯想知道有什么不一样

 
git log dev...master

显示出每个提交是在哪个分支上

 
git log --left-right dev...master

模板:Leftrightflex

(来源摘自互联网示例,作者 未知)

分支分叉

查看 commit 冲突

[jian@kang /home/kwy]$
git cherry origin/newsearch
+ 238d26d28bf26f3e6239f611eef0b4ba022ccc2b
+ 4e73c503c36a32bd3898d0852d2345876c336110
+ f3ca675e3099d5a78356a4b7956cd1f009256425
+ 4679bcbf787aec6305774e43d7576668d6bb1fe1
+ 7e66f0c6e3db96635fa7e8b605ed3dcd1e3ac246
+ 8adc688966932c773d34f965bb6a67d3f4027031
+ 8b7515bfab1f5f30982c9f91a4b4ee078bba584e
+ 807eabc35f5b4c2b5bfcbe454336ec00e5f21a4c
+ c0cbbc52bdb1f64d6e5f77009539d34ef9a95703
+ 141a349c4a582c96a687dda5e753fe142d39530e
+ 528b5078279a81ecb3689d90545f79a0967c7741
+ 25d62a2e9150369dcf6e920155765b958b9667e3
+ 75177225255744f17883db36499fd3d84e13d141
+ c9e5296465d0d8c8d947d106b878172b0b8e46bc
+ ae361a1cb6b9761c0d66d8b8012bc788bb79bb87
+ 3cc4dbff74ff6aec5d860fa0c3b7bbb9c1a597eb
+ c981f2493df605040687ba5eb72cb5d1bda081d5
+ bebcd79093663b20311cd37f5a35be1bf187814a
+ 9a0bffe2dc669c708a37cdfe88fe083d8ac9cc62
+ 0edc40b2f6972a99615e15b03b6b06ef545f2618
+ f210117a590d1bbe544c41633515c73585bee098
+ d4e8ade08e51e99079a3d53459ac79e6a22971f0
+ 5dd235085f3772ea9aa5ed8e7ebf1581070182fd
+ b9dffb8828520ef77bc1ac316e6ab6842905711b
+ 0670cf29a0110217aaea9c450d2d346f1c86801b
+ a2ca7db00ef3f2d9600ac1e435261874c746945c
+ 672f33756bf4ecb3c65fb9a1b972f1306677911c

回退到冲突的前一个 commit

 
git reset --hard <commitID>

cleanup

所有被 ignore(忽略)的本地文件会被全部删除,而且是无法恢复的,因为远程仓库里根本就没有这些文件。

gitee

多账号

git clone git@gitee.com:bjz***/askgo-admin-fe.git

.gitignore

.gitignore

文件夹

今天遇到一个问题,在一个项目里,我添加了一个文件夹,然后 commit,后续考虑其他事情,又在移除了这个文件夹,commit,然后 push,结果造成在 gitlab 中,线上网站存在这个文件夹,而本地没有这个文件夹。

使用强删解决这个问题:

 
git rm -r --cached photoshop-plugin-ts
git commit -m "删除photoshop插件"
git push -u origin kk_test

迁移

查看当前仓库地址。

/home/kangkk
git remote -v

git push --mirror

git remote set-url

迁移单个分支。

[jian@kang /home/kangkk]$
git remote set-url origin ssh://git@192.168.31.5:10022/abc/xyz.git
/home/kangkk
git push

迁移所有分支。

[jian@kang /home/kangkk]$
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
枚举对象中: 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'。

检查仓库地址更改状态。

[jian@kang /home/kangkk]$
git remote show origin
* 远程 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 合并