Skip to main content

Git命令

MarshioAbout 4 mingit

init

初始化仓库

git init

remote

远程仓库信息

# 查看远程仓库信息
git remote -v

# 绑定指定仓库
git remote add origin [email protected]:demo/spring.git

# 修改远程仓库地址
git remote set-url origin [email protected]:imarshio/marshio.git

commit

提交改动

git add .

# 提交所有已修改文件
git commit -a -m "new message" 

# 提交指定文件

push

推送提交

git push 

删除分支

# 删除本地分支
git 

# 删除远程分支
git push origin --delete branch-name

pull

拉取推送,更新分支

git pull

branch & chekout & switch

分支 切换分支

# 新建分支
git branch news_branch_name

# 切换分支
git checkout new_branch_name
# 等价于
git switch new_branch_name

# 以上两条命令可以简写为
git checkout -b new_branch_name
git checkout -B new_branch_name

# 删除分支
git branch -d old_branch_name
# 如果要删除的分支存在未完成的merge,需要替换成如下命令
git branch -D old_branch_name

# 切换并自动追踪远程分支
git checkout remote_branch_name
# 等同于如下命令
git checkout -b remote_branch_name --track origin/remote_branch_name

# 修改还没被commit 的时候,撤销本地修改
git checkout HEAD -- /path/to/file

git checkout branch_name --

重命名分支

# 重命名本地分支
git branch -m new-branch-name

# 重命名其他分支
git branch -m old-branch-name new-branch-name


# 重命名远程分支,先删除远程分支,在将本地分支推送到远端
git push origin --delete old-branch-name
git push origin new-branch-name
git branch --set-upstream-to=origin/new-branch-name new-branch-name

追踪远程分支

# 完整命令
git checkout -b remote_branch_name --track origin/remote_branch_name
# 简写
git checkout remote_branch_name

# 如果不加 --track 的话,是不会自动追踪远程分支的
# 你可以通过 git branch -vv 来查看
# 如下可以看到 v1.0.0 是没有追踪远程分支的,main 是有追踪远程 main 分支的,且可以看到落后了 228 个提交,最后一次提交记录是 refine .gitignore
git branch -vv
* feature/v1.0.0 779c02c for debug
  main           79a5e26 [origin/main: behind 228] refine .gitignore

# 如果发现有分支没有追踪远程分支,则需要通过如下命令进行修复,当然如果没有影响也可以选择不管
git branch --set-upstream-to=origin/feature/v1.0.1 feature/v1.0.1
# 如果你已经在这个分支上了
git branch -u origin/feature/v1.0.1

# 远程追踪的其他好处
git pull origin feature/v1.0.1
# 可以简写成
git pull

merge

合并,会保留另外一个分支的提交记录,适合团队开发。

git merge another_branch_name

# 如果有冲突的话,在本地解决冲突

# 解决冲突后,使用add 标记文件为已解决

# 之后commit
git commit -m "merge & resolve conflict"

rebase

变基,会在另外一个分支的最新提交前面追加当前分支的提交,使提交的历史显得更具线性话,适合个人开发、个人分支。

stash

贮存,暂存当前分支的改动,而不用在切换到其他分支时污染其他分支,或在更新分支时远程推送版本与本地版本有冲突而不能成功更新

场景:比如当前你正在dev分支开发新的需求,突然来了一个临时需求,需要在master分支上新拉一个hot fix分支,但是你当前的需求又马上快完成了,你不想丢掉本地的提交(假设此时,你的本地代码还没推送到远程仓库),那么我们可以使用stash来保证代码的干净。


# 直接调用会将当前所有的修改暂存到一个stash中
git stash

git stash 

cherry-pick

背景

假设我当前在A分支进行了一次提交,然后我想在B分支进行同样的提交操作,但是不想重新写代码,此时就可以通过 cherry-pick 完成。

# A分支模拟操作
$ git commit -m "test commit"

查看记录

# 查看A分支的提交日志,需要注意这个 commit id,后面会用到
$ git log
commit 866a5ba7fdd81b49662fa47edb068d42d6059b2c (HEAD -> A, origin/A)
Author: demo <[email protected]>
Date:   Thu May 23 17:13:20 2024 +0800

    test commit

commit 6e12f87ec155fc308c10243ec1f8ad35ad345a71
Author: demo <[email protected]>
Date:   Mon May 13 10:08:06 2024 +0800

    pre test commit

提取

# 切换到目标分支B
$ git checkout B

# cherry-pick 后面跟的就是提取的 commit id
$ git cherry-pick 866a5ba7fdd81b49662fa47edb068d42d6059b2c
[B a18b13c] add type column
 Date: Thu May 23 17:13:20 2024 +0800
 2 files changed, 2 insertions(+)

# 确认
$ git log
commit a18b13cfca69a1cbbc1e5b47ebee9f43a60bb0a7 (HEAD -> pre)
Author: demo <[email protected]>
Date:   Thu May 23 17:13:20 2024 +0800

    test commit

fetch


# 将远程仓库的更新下载到本地仓库,但不会直接更新本地分支的代码
git fetch origin 

config


# 查看key值
git config key

# 查看本地key值
git config --local key 

# 查看所有配置,回车查看下一行,q 推出
git config --list

# 设置pull动作为合并(merge),而不是变基(rebase)
git config pull.rebase false

revert && reset

reset

# 沿着 HEAD 往前走1步
git reset [--hard | --soft] HEAD~1
git reset [--hard | --soft] <CommitID>

# 然后强推到远程分支
git push origin feature/v0.0.1 --force

revert

# 只回退一个commit
git revert HEAD
git push origin <你的分支名>

# 回退多个commit,
# -n (or --no-commit):把所有的撤销改动放进你的暂存区
# <旧的hash>..<新的hash> 是一个左开右闭区间
git revert -n <旧的hash>..<新的hash>