本文最后更新于 2025年3月27日 晚上
【软件开发】Git 概念与常用命令
Git 概念
存储方式
Git 是分布式存储,每一个 clone 下来的仓库都可以看成独立的个体,只是 Git 有提供同步功能,因此 Git 支持离线使用,因为本质上本地和云端是两个仓库。
仓库构成
提交(commit)
提交是存储文件的最基本元素,记录了每次对文件的修改信息,同时也是仓库文件状态判断的基点。多个提交对文件的修改可能是顺序进行也可能是同步进行,同步的结果往往会导致冲突。
分支(branch)
分支引用了提交并将其串成一条时间线,外界也由此得以查看提交。不同分支引用的时间线可能重叠也可能分离,形成一个树状结构,但最终分支都是要合并的,这也意味着所有提交最终都会形成一条唯一的时间线。
子模块(submodule)
子模块是对其他仓库中的引用,具体而言是引用了分支-提交,对本仓库而言它也是一种受版本控制的文件,文件中存放着引用信息,所以和正常文件一样,修改时要添加到暂存区,删除时也要从仓库区中移除。
子模块所引用的分支版本信息可以从.git/index 文件中找到。
标签(tag)
标签实质是给提交起一个别名从而方便识别,这是一个可选功能,通常人们会借此把一些关键的提交打上标签,如记录版本号和发布信息。
远端(remote)
远端是用于同步的其他仓库,以便备份或多人协助,因为是分布式存储,每个仓库都是独立的个体,所以不提供远端也是可以的。
工作区与暂存区
这两个区域是本地工作用仓库独有的内容,在云端的裸仓库中没有这两者的存在。这主要是为了安全和便于工作使用的临时区域,每一次的文件修改都要按顺序提交到每个区域,当仓库指向的提交变更时它们便会被重置。
- 工作区:用户编写代码的地方,文件资源管理器能直接看到的部分。
- 暂存区:提交代码前必须先将工作区的所有的文件添加到暂存区。
- 仓库区:将暂存区的代码修改变成提交,真正的存储到仓库中。
常用命令
通用项
选项
- HEAD:表示最新的一次提交。
- HEAD~1:表示最新提交的前一次提交。
- -h:任何命令加该选项,可以快速查看使用方式。
- –help:任何命令加该选项,可以查看命令的详细手册。
规则
仓库初始化和配置
1 2 3 4 5 6 7 8
| git init [--bare] git clone --recursive <url>
git config <name> git config [--global] <name> <value> git config [--global] --unset <name> git config [--local] --list
BASH
|
仓库信息查看
1 2 3 4 5 6 7
| git branch -a git submodule git log git tag git status git diff git reflog
BASH
|
存储区处理
1 2 3 4 5 6 7 8 9 10 11
| git ls-files [--cached]
git add <file> git rm [--cached] <file>... git restore [--staged] <file> git commit -m '<message>'
git stash git stash pop
git reset [--hard] <commit>
BASH
|
分支处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git branch -a git branch <new-branch> git branch <new-branch> <commit> git branch -m <new-name> git branch -d <branch> git branch -u <up-branch> <branch>
git checkout -b <new-branch> <branch> git checkout --orphan temp git switch -c <new-branch> <commit> git switch <branch> git switch --detach <commit>
git rebase -i <commit> git rebase <branch> git merge <branch>
BASH
|
子模块处理
1 2 3 4 5 6
| git submodule add <url> <path> git submodule init git submodule update git submodule status git submodule set-url <path> <url> git submodule deinit <path>
BASH
|
标签处理
1 2
| git tag -f <tagname> [<commit>] git tag -d <tagname>
BASH
|
远端处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git remote git remote show <name> git remote add <name> <url> git remote rename <name> <new-name> git remote remove <name> <new-name> git remote set-url <name> <url> git remote set-url {--add|--delete} <name> <newurl> git remote set-head <name> {-a|<branch>} git remote prune <name>
git fetch git pull git push [--force] git push <remote> -d <branch>
BASH
|
常用操作
处理网络代理
1 2 3 4 5 6
| git config --global http.proxy <url> git config --global https.proxy <url>
git config --global --unset proxy.http git config --global --unset proxy.https
BASH
|
彻底删除子模块
1 2 3
| git submodule deinit <path> git rm <path> rm -rf .git/modules/<path>
BASH
|
撤回上次提交版本
1 2
| git reset HEAD~1 git push --force
BASH
|
清除所有历史记录
1 2 3 4 5 6
| git checkout --orphan temp git branch -D <branch> git branch -m <branch> git add . git commit -m '-' git push --force
BASH
|
清除工作区中所有未被跟踪的文件
无法撤销,用前一定要备份!
gitignore 文件
书写格式
-
每行一个忽略项。
-
支持 glob 通配符。
-
限定忽略项类型:
- 文件或目录:只填完全名称,如
test
、text.txt
。
- 仅目录:添加
/
后缀,如test/
。
-
限定忽略项位置:
- 从当前目录匹配:带
/
前缀,如/test
。
- 从任意目录匹配:不带任何前缀,如
test
。
-
添加!
前缀取消忽略,如!test.txt
。
注意如果整个目录都被忽略了,那没法生效。
取消跟踪
gitignore 只能忽略未跟踪文件,如果文件已被跟踪需先从仓库中删去。
1
| git rm --cached <file>
BASH
|
参考资料