Hello World

Just So So ...

HOW TO GIT

Git 0 评


question.png

➡️ 服务器自动pull

对于个人项目,经常需要小修小改,每次push后都要远程登录服务器进行pull,太折腾了。因此利用git的webhook功能自动部署。

1、准备阶段

  • 权限问题

因为webhook请求服务器URL是以www用户执行的,因此www要有pull的权限
参考 https://b.liwh.net/index.php/archives/105 开启www登录

PHP代码需要运行shell_exec函数

  • 以SSH方式pull代码

2、搭建webhook

  • 搭建网站

webhook.php代码:

<?php
$cmd = "cd /www/wwwroot/xxx && git pull";
echo shell_exec($cmd);
  • 建立webhook

以Coding为例,在开发者选项->Service Hook中新增webhook,请求地址填正确即可。

??? 愉快地码吧


question.png

刚刚提交的时候漏 add 了文件

git reset HEAD^

如果是好几次提交前的呢?待更...


question.png

通过 Git 生成补丁文件,保持目录结构

git diff --diff-filter=ACMR --name-only d134303 3d3b6e9 | xargs tar -zcvf ~/Downloads/patch-3d3b6e9.tar.gz

需要注意一点!commit id 的先后顺序,最新的在后面


question.png

如何可以让git pull不用输入用户名密码

首先需要通过 SSH 的方式进行连接,而不能通过 https 的方式连接。
如果此前已经把 origin 的 URL 设置为 https,则可以通过命令行修改:

git remote set-url origin git@git.dev.tencent.com:aszx0413/repo.git

设置 SSH 公钥可参考:https://help.coding.net/docs/project/features/ssh.html
其中涉及几个命令:

## 生成公钥
ssh-keygen -t rsa -C "34378706@qq.com"

## 查看公钥
cat /root/.ssh/id_rsa.pub

## 测试连接
ssh -T git@e.coding.net
ssh -T git@gitee.com

如果存储公私钥的文件路径不是默认,则会引发问题。解决方案参考:
https://juejin.im/post/6844904161088110606
https://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent
仔细阅读!

以下是不用每次 ssh-add 的解决方案:

https://stackoverflow.com/questions/69488006/i-need-to-re-run-evalssh-agent-and-ssh-add-on-every-boot


question.png

解决git clone时报错:The requested URL returned error: 401 Unauthorized while accessing

版本问题,最直接的解决办法就是重新编辑安装git吧:

  1. 下载:# wget -O git.zip https://github.com/git/git/archive/master.zip
  2. 解压:# unzip git.zip
  3. 进入git目录:# cd git-master
  4. 编译安装:

autoconf
./configure --prefix=/usr/local
make && make install

  1. 最后别忘了删掉旧的git,并把新版本的git建立软链接到/usr/bin/git

rm /usr/bin/git
ln -s /usr/local/bin/git /usr/bin/git


question.png

取消提交日志中的 Merge XXX

可以让版本日志清新

New Repository

  • 使用coding.net
  • git客户端使用SourceTree

注意事项

  • Rebase前确认远程分支是否有别的修改,即刷新远程分支状态:Repository - Refresh Remote Status
  • git rebase origin对应SourceTree的操作为 Repository - Interactive Rebase

question.png

取消所有修改,回到上一次 git commit

git checkout . && git clean -df

git clean removes all untracked files (warning: while it won't delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and git checkout clears all unstaged changes.

你现在的工作区是一片清净,然后,你需要 npm 或 composer 一个包,从而令到你的项目中各个目录增加了一堆文件,并且基于新增的库进行了编码开发,修改了多个代码文件。

最后,你发现这个新增的包完全不适用于该项目。怎么?

执行上面命令,还你一片清净


question.png

git 出现 fatal: refusing to merge unrelated histories 错误

其实这个问题是因为两个根本不相干的 git 库,一个是本地库,一个是远端库,然后本地要去推送到远端,远端觉得这个本地库跟自己不相干,所以告知无法合并。

解决方案:

git pull origin master --allow-unrelated-histories

question.png

如何停止 git 一个已经被 git 的文件

这问题听上去都拗口。意思是,当 a 文件已经被 git 了,你才把 a 添加到 gitignore 是没有用的。再修改 a 文件,通过git status发现,仍然有 a 被 modified 的信息。

正确的做法是:把 a 移除(不是永久删除,等会需要移回来),然后 commit 一次,再把 a 移回来。此后修改 a 文件,也不会被 git 跟踪了。


question.png

现在项目已经编写一定的源码,此时,为了备份/后续团队开发等等原因,需要把项目 git 起来

git status检查确认,项目没有任何 git。

$ git status
fatal: Not a git repository (or any of the parent directories): .git

初始化 git:git init

以 Coding.net 为例,需要把项目 git 到私有空间。参考 https://coding.net/help/doc/git/import-from-local.html

但在执行git push -u origin master会报错:

error: src refspec master does not match any.

原因是 git 工作目录 .git 是空的,因为没有git add任何东西,也没有git commit过。

git add .之后,git commit

再执行git push -u origin master

大功告成!


question.png

如何停止 git 一个已经被 git 的目录

现在问题来了...
刚才不小心把 composer 的目录 vendor 也加入了 git,此目录不应该加入到版本控制,但本地开发需要有该目录。

首先建立 .gitignore 文件,把 vendor 加入忽略目录。
然后移走 vendor 目录,为什么是移走而不是删除?因为 commit 之后要移回来。
git commit,此时变化的有 .gitignore 文件和删除 vendor 目录
把 vendor 目录移回来
再跑 git status,世界都清静了

发表评论
撰写评论