git学习笔记1

1、如何修改上一次git commit提交的信息

# 1.未push,调整上一次commit的信息
# 将最新的本地提交信息修改为 "xxx",并保持原有提交时间、作者、父提交等信息不变。
git commit --amend -m "xxx"

# 2.已push,调整上一次commit的信息
# 将最新的本地提交信息修改为 "xxx",并保持原有提交时间、作者、父提交等信息不变。
git commit --amend -m "xxx"
# 强制推送修改后的提交
git push --force-with-lease

参考资料

2、

# 设置全局git配置中的用户邮箱
git config --global user.email "[email protected]"
# 设置全局git配置中的用户名
git config --global user.name "jiakai"
# 用于列出全局 Git 配置中的所有设置
git config --global --list

3、恢复前x次误删的文件

# 查看仓库的提交历史
git log
#  命令从 x+1 提交中恢复误删的文件
git checkout <commit-hash-of-x+1> -- 误删文件名

4、git pull报错You have divergent branches and need to specify how to reconcile them.的解决方案

错误信息:

git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

得到的这个 "hint" 消息是 Git 为了提醒你,你没有设定 pull 的默认行为。当你在 git pull 的时候, 你可能要合并 divergent 的分支。Git 提供了几种处理 divergent branches 的方法,包括 merge,rebase 和 fast-forward。 你可以根据你自己的情况来设定默认的 pull 行为。

git config pull.rebase false  # merge (default strategy)
git config pull.rebase true   # rebase
git config pull.ff only       # fast-forward only
  • merge:这是默认策略。Git 将两个分支的代码进行合并。如果有冲突,Git 会提示你手动解决冲突。
  • rebase:Git 将你的分支上的每一次提交都分别应用到上游分支上。这使得历史记录更加线性。
  • fast-forward:只有当你的分支落后于上游分支时,Git 才会更新你的分支。如果你的分支领先于上游分支,Git 不会做任何事情。

如果你想要为所有的仓库设置一个默认的行为,你可以用 --global 选项:

git config --global pull.rebase false  # merge (default strategy)
git config --global pull.rebase true   # rebase
git config --global pull.ff only       # fast-forward only

另外,你也可以在 git pull 的时候用 --rebase,--no-rebase,或者 --ff-only 选项来覆盖默认的行为。

# 先获取远程仓库的最新更改,然后将你在本地分支上的更改应用在这些更新之上。
# 这样做的好处是,你的本地更改总是基于最新的代码,而不是你上次拉取的旧版本。
# 它能够保持你的提交历史更加线性和清晰。
git pull --rebase
git pull --no-rebase
git pull --ff-only

注: git pull --rebase 命令可能会导致冲突,特别是当你和其他人都在修改同一个文件时。这种情况下,你需要手动解决冲突后,再继续进行 rebase 操作。

chatgpt解释

chatgpt解释:

chatgpt解释

关于rebase和merge的选择:

关于rebase和merge的选择

5、如果最后一次提交中包含敏感数据,则可以通过以下方式解决:

# 将.env.example文件添加到暂存区
git add .env.example
# 修改最近一次提交。这个命令将刚刚添加的.env.example文件包含到最近一次提交中,而不是创建一个新的提交。这样做的目的可能是为了保持提交历史的整洁。
git commit --amend
# 强制推送所有分支和标签到远程仓库
git push --all --tags --force

更推荐的做法是重置敏感数据。

6、dependabot更新package.json后报错的解决方案

Run pnpm install --frozen-lockfile
Lockfile is up to date, resolution step is skipped
 ERR_PNPM_OUTDATED_LOCKFILE  Cannot install with "frozen-lockfile" because pnpm-lock.yaml is not up to date with package.json

Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"
Error: Process completed with exit code 1.

解决方案:

pnpm install
git add .
git cz
git push

报错原因:package.json和pnpm-lock.yaml文件的版本不匹配。

错因

7、查看git版本信息

git --version

8、远程仓库创建了MIT许可证,本地无法push的解决方案

# "git pull" 命令从远程仓库获取更改到本地仓库。
# "origin" 是 Git 默认为你克隆的远程仓库命名的。
# "main" 是你要获取的分支。
# "--allow-unrelated-histories" 选项允许合并两个历史不相互关联的分支。
git pull origin main --allow-unrelated-histories

# "git push" 命令将本地仓库的更改推送到远程仓库。
# "origin" 是你要推送到的远程仓库。
# "main" 是你要推送的分支。
# "-u" 选项设置上游,这样在下次推送或拉取时,如果没有指定分支,Git 会默认使用这个分支。
git push origin main -u

9、Divergent branches是啥?

Divergent branches是啥?

10、关于git pull和git rebase的解释

  • git pull:这个命令用来从远程仓库拉取最新的更改。它其实是 git fetch 和 git merge 命令的组合。

git fetch 命令会获取远程仓库的最新更改,但不会应用这些更改;git merge 命令则将这些更改合并到你的本地分支。

  • git rebase:这个命令用来移动或者合并提交。它会获取你在本地分支上做的更改,并将这些更改应用在你指定的其他分支上。

10、更改git中显示的日期格式

# 将当前用户所有的 Git 仓库的日志日期格式设置为 ISO 8601 格式
# 原来的日期格式是 Git 默认的格式,被称为 "RFC 2822" 格式,它基于电子邮件头部的日期标准。
# 原本的:Date:   Mon Jun 5 20:37:39 2023
# 改为iso后的:Date:   2023-06-05 05:37:39 -0700
git config --global log.date iso