【贴士】Git tips‍

Git tips

入门篇——安装和配置

  1. 下载@Git官方下载网站
  2. 安装(略)
  3. 初始化
# 设置用户名和邮箱
git config --global user.name "Your Name"
# 设置邮箱
git config --global user.email "[email protected]"
# 查看已经设置的全局变量
git config --global --list
# 设置SSL认证(这会让Git忽略对HTTPS 服务器SSL证书的有效性检查,这样可能会有安全风险。)
git config --global http.proxy http://username:password@proxy:port

日常篇——常用指令

Git指令官方指引
https://git-scm.com/docs/giteveryday

查看远程仓库的url

git remote -v

Git初始化

git init

Git查看分支

git branch -l #查看本地分支
git branch -r #查看远程分支

查看远程分支、本地和远程分支的pull、push映射

git remote show origin

撤回commit

git reset --soft HEAD

这个命令不加任何参数,会从当前分支直接创建分支!如果当前分支有commit,commit有可能丢失

git checkout 

切换分支

git checkout -b local_branch_name origin/remote_branch_name
git switch -c local_branch_name origin/branch_name

合并到当前分支

git merge targe_branch

推拉结合

git pull remote_branch_name local_branch_name
git push remote_branch_name local_branch_name

当远程分支和当前分支名字不一致时

git push origin HEAD:remote_branch_name

这个命令会列出你的本地仓库的引用日志

git reflog

看看git log

git log
git log branch_name

cherry pick
将某个(或多个)提交从一个分支复制到另一个分支,而无需合并整个分支的历史记录。

  1. 将某个(或多个)提交从一个分支复制到另一个分支,而无需合并整个分支的历史记录。
# 切换到目标分支
git checkout <目标分支>
# 查看源分支的提交记录
git log --oneline <源分支>
# 选择你钟意的cherry-pick命令
git cherry-pick <提交哈希值>
git cherry-pick <起始提交哈希值>^..<结束提交哈希值>
git cherry-pick <提交1哈希值> <提交2哈希值> <提交3哈希值>

stash命令

  1. 保存当前工作:

    git stash save "your stash message"
    

    这将保存当前工作目录中的所有未提交的更改。可选的消息参数可以用于描述保存的内容。

  2. 查看保存的工作:

    git stash list
    

    这将显示已保存的工作目录状态的列表。每个 stash 都有一个唯一的标识符(stash@{n})和一个相关的消息。

  3. 恢复保存的工作:

    git stash apply {0/1/2/3/4/5/.....}
    # eg.
    git stash apply 0
    

    这将应用指定的 stash。如果你有多个 stash,需要使用相应的标识符(n 是 stash 的索引)。

  4. 恢复并删除 stash:

    git stash pop
    

    这将应用最近保存的 stash,并从 stash 列表中移除。相当于 git stash applygit stash drop 的组合。

  5. 删除 stash:

    git stash drop stash@{n}
    # eg.
    git stash drop stash@1
    

    这将从 stash 列表中移除指定的 stash。

  6. 清除所有 stash:

    git stash clear
    

    这将删除所有保存的 stash。

翻车篇——If you fked up

放弃本次分支合并

git merge --abort

要删除本地 Git 分支,可以使用以下命令:

# 删除之前检查分支的更改已经被合并到当前分支或其他分支
git branch -d local_branch_name
# 强制删除
git branch -D local_branch_name

修改远程分支commit message

git commit --amend -m "message"
git push --force-with-lease origin HEAD:remote_branch_name

将commit回滚到add状态

git reset --soft HEAD^

如何回滚本分支上某个文件的所有提交

  1. 查看文件的历史记录:首先,查看该文件的提交历史,以便确定从何时开始回滚。

    git log -- <文件路径>
    
  2. 选择目标提交:找到你希望回滚到的那个提交哈希值(称为commit-hash)。

  3. 检出目标提交的文件版本:检出该文件在目标提交中的版本,但不要更新工作目录的其他部分。

    git checkout <commit-hash> -- <文件路径>
    
  4. 提交更改:将该文件的回滚版本作为一次新的提交。

    git add <文件路径>
    git commit -m "Revert <文件路径> to version from "
    

这将把你指定的文件恢复到某个特定提交的状态,并将其作为新的提交记录在当前分支上。

例如,如果你想将文件example.txt回滚到提交哈希值abc123的状态,你可以执行以下操作:

git log -- example.txt  # 查看文件的提交历史
git checkout abc123 -- example.txt  # 检出文件的旧版本
git add example.txt  # 添加文件到暂存区
git commit -m "Revert example.txt to version from abc123"  # 提交更改

要删除本地 Git 分支,可以使用以下命令:

git branch -d local_branch_name

修改远程分支commit message

git commit --amend -m "message"
git push --force-with-lease origin HEAD:remote_branch_name

将commit回滚到add状态

git reset --soft HEAD^

** 回滚分支 **

(推荐方式)

	# 一般回滚
	git revert commit_hash
    # 回滚merge操作 
	git revert -m 1 commit_hash

(不推荐方式)
要将远程分支回滚到特定版本,你可以使用 git push 命令来强制推送本地分支,覆盖远程分支的历史。以下是一般的步骤:

# 
#请注意:强制推送会改变远程分支的历史,可能会导致其他人的工作受到影响。确保你的团队了解并同意进行这样的操作。此外,推送前最好备份远程分支,以防发生意外。
git checkout <your-branch>  # 切换到要回滚的本地分支
git reset --hard <commit-hash>  # 回滚到特定版本, 是目标版本的提交哈希值
git push origin <your-branch> --force
git log  # 确认本地分支已经回滚到指定版本

  • 当reset的commit hash不是属于本分支的,当前分支的HEAD指针和分支指针将会移动到指定的 commit_hash,即另一个分支的某个提交

影响

  • 本地更改丢失:所有未提交的更改和暂存的更改都会丢失。
  • 历史记录改变:分支A的历史记录将会改变,现在它只包含从reset的hash值对应的提交开始的历史。
  • 远程分支:如果你将这种变化推送到远程仓库(使用 git push --force),远程分支A也会被重写为指向 jkl1121,这会影响其他协作者的本地仓库。

如何连续回滚多个commit

Clean way which I found useful

git revert --no-commit HEAD~3..
git commit -m "your message regarding reverting the multiple commits"

这条命令会生成最新的3个提交的反向提交,意味着删除了的代码会恢复,新增的代码会删除,更新的代码会恢复原样
This command reverts last 3 commits with only one commit.

Also doesn’t rewrite history, so doesn’t require a force push.

The … helps create a range. Meaning HEAD~3… is the same as HEAD~3…HEAD

** Push的时候卡住 **

  1. 推送的文件大小太大
  2. 推送的文件有太多行(如img src太长,占了很多行)
    可以使用如下命令
git config --global http.postBuffer 157286400

引用

https://stackoverflow.com/questions/15843937/git-push-hangs-after-total-line

你可能感兴趣的:(git)