Git 常用命令
Git 是一个分布式版本控制以及源代码管理工具,它可以为你的项目保存若干快照,以此来对整个项目进行版本管理。
init
初始化一个新的 Git 版本库,这个版本库的配置、存储等信息会被保存到当前目录的.git
文件夹中。
config
修改 Git 配置,可以是版本库的配置,也可以是系统或全局配置。
1 2 3 4 5 6
| git config --global user.email git config --global user.name
git config --global user.email "test@gmail.com" git config --global user.name "Test"
|
更多设置
help
Git 帮助文档,可以供我们快速查阅。
1 2 3 4 5 6 7 8 9 10 11
| git help
git help -a
git help add git help commit git help init
|
status
显示当前工作空间的状态信息。
1 2 3 4 5
| git status
git help status
|
add
添加文件到当前工作空间。
1 2 3 4 5 6 7 8
| git add test.c
git add /path/to/file/test1.c
git add ./*.py
|
branch
管理分支,可以通过命令对分支进行增删改查。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git branch -a
git branch myNewBranch
git branch -d myBranch
git branch -m myBranchName myNewBranchName
git branch myBranchName --edit-description
|
checkout
1 2 3 4 5 6
| git checkout
git checkout branchName
git checkout -b newBranch
|
clone
1 2
| git clone https://github.com/liuyuhe666/liuyuhe666.git
|
commit
1 2
| git commit -m "feat: add login"
|
diff
显示当前工作区和提交的不同。
1 2 3 4 5 6 7 8
| git diff
git diff --cached
git diff HEAD
|
grep
在版本库中进行快速查找。
1 2 3 4 5
| git config --global grep.lineNumber true
git config --global alias.g "grep --break --heading --line-number"
|
1 2
| git grep 'variableName' -- '*.java'
|
log
显示版本库的提交日志。
1 2 3 4 5 6 7 8
| git log
git log -n 10
git log --merges
|
merge
合并分支。
1 2 3 4 5
| git merge branchName
git merge --no-ff branchName
|
mv
重命名或移动一个文件。
1 2 3 4 5 6 7 8 9
| git mv HelloWorld.c HelloNewWorld.c
git mv HelloWorld.c ./new/path/HelloWorld.c
git mv -f myFile existingFile
|
pull
从远程库中拉取代码。
push
rebase
1 2 3
|
git rebase main newBranch
|
详细介绍
reset
1 2 3 4 5 6 7 8 9 10 11 12 13
| git reset
git reset --hard
git reset 31f2bb1
git reset --hard 31f2bb1
|
rm
1 2 3 4 5
| git rm HelloWorld.c
git rm /pather/to/the/file/HelloWorld.c
|
参考资料