基础操作

仓库初始化

1
git init

克隆远程仓库

1
git clone <url> xxx

查看仓库状态

1
git status

添加文件到暂存区

1
2
3
git add <file>

git add .

提交更改

1
2
3
git commit

git commit -m "commit message"

查看提交历史

1
git log

分支基本操作

查看本地分支

1
2
3
git branch

git branch -v

创建新分支

1
git branch <branch_name>

切换分支

1
git switch <branch_name>

重命名分支

1
git branch -m master main

删除分支

1
git branch -d <branch_name>

查看相对于当前分支的未合并分支

1
git branch --no-merged

分支合并操作

普通合并

1
2
git checkout master
git merge iss53

遇到冲突时直接撤销合并

1
git merge --abort

变基合并

1
2
git checkout experiment
git rebase master

遇到冲突时直接撤销变基

1
git rebase --abort

压缩提交历史

1
2
3
git rebase -i HEAD~4

# ...

撤销操作

撤销工作目录的修改(基于index重置)

1
2
3
git restore filename

git restore .

撤销index的修改(基于HEAD重置),但不改动工作目录中的内容

1
2
3
git restore --staged filename

git restore --staged .

撤销工作目录和index的修改(基于HEAD重置),改动影响工作目录中的内容

1
2
3
git restore --staged --worktree filename

git restore --staged --worktree .

修正上一次的提交

1
git commit --amend

远程仓库基础操作

添加远程仓库为orign

1
git remote add origin <repo_url>

查看远程仓库

1
git remote -v

查看远程仓库详细信息(联网更新)

1
git remote show <remote_name>

重命名远程仓库

1
git remote rename <remote_name_old> <remote_name_new>

移除远程仓库

1
git remote remove <remote_name>

远程仓库同步操作

获取远程仓库的信息(但是不会合并)

1
2
3
4
5
git fetch

git fetch origin

git fetch --all

这几个命令只在存在多个远程仓库时有区别。

拉取远程仓库的分支

1
git pull <remote_repo> <remote_branch>:<local_branch>

推送远程仓库的分支

1
git push <remote_repo> <local_branch>:<remote_branch>