背景
在我们的日常开发中,接触最多的代码管理工具就是 git,下面咱们就一一列举一下经常使用到的 git 命令
常用命令
一、创建一个工作区
1. 克隆仓库
$ git clone xxx.github.com
2. 初始化空 Git 仓库
$ git init
二、协作
1. 推送分支到远程
$ git push origin main
2. 拉取远程分支
$ git pull origin main # 拉取远程分支到本地,commit记录在当前记录上叠加
$ git pull --rebase origin main # 拉取远程分支到本地,以rebase的形式整理commit信息
3. 下载远程仓库所有更新
$ git pull origin main # 拉取远程分支到本地,commit记录在当前记录上叠加
$ git pull --rebase origin main # 拉取远程分支到本地,以rebase的形式整理commit信息
配置信息
1. 查看 git 配置
$ git config --list # 查看所有的配置信息
$ git config user.name # 查看当前项目git配置中的user.name
$ git config --global user.name # 查看全局git配置中的user.name
2. 设置 git 配置
$ git config user.email "hello_world@163.com" # 设置当前项目git配置中的user.email
$ git config --global user.email "hello_world@163.com" # 设置全局git配置中的user.email
# user.email 为 git config --list 罗列出来的设置项中的一项。罗列出来的配置项,均可通过上述命令进行修改
3. 设置上游代码源
$ git remote add upstream https://github.com/IDuxFE/weekly.git # 设置上游代码源
4. 查看上游代码源
$ git remote -v
三、处理当前工作区的修改
1. Reset
$ git reset HEAD^ # 回退当前仓库的最新的一条commit,默认软回退,在代码管理器,还能看到该commit修改的文件
$ git reset HEAD^ --hard # 回退当前仓库的最新的一条commit,该commit修改的文件也直接被重置
$ git reset HEAD~3 # 从当前仓库的最新的一条commit开始,一共回退3条commit的更改
$ git reset 9334861 # 回退指的某一条commit
2. Commit
$ git commit -m "feat: hello_world"
四、审查修改历史和变动状态
1. 查看项目 git commit 日志
$ git log # 展示完整的的git commit日志,包含commit的标题、内容、作者和提交时间等等
$ git log --oneline # 仅展示commit的标题
2. Reset
$ git reset HEAD^ # 回退当前仓库的最新的一条commit,默认软回退,在代码管理器,还能看到该commit修改的文件
$ git reset HEAD^ --hard # 回退当前仓库的最新的一条commit,该commit修改的文件也直接被重置
$ git reset HEAD~3 # 从当前仓库的最新的一条commit开始,一共回退3条commit的更改
$ git reset 9334861 # 回退指的某一条commit
3. Rebase
git rebase -i HEAD~3 # 从当前仓库的最新的一条commit开始,一共rebase3条commit的更改
4. Cherry-pick
$ git cherry-pick 9334861 # 遴选某一条commit
$ git cherry-pick 9334861...8884861 # 遴选一个区间的commit,开区间,不包含9334861这个commit
5. Merge
$ git merge origin main
6. Tag
$ git tag -a v1.0.0 dc89a360 -m "备注信息" # 创建一个tag,名称为v1.0.0,基于commit节点dc89a360,备注信息“备注信息”
$ git tag # 列出所有的tag
$ git tag -d v0.1.1 # 删除tagv0.1.1
$ git push origin v0.0.1 # 推送tag到远程仓库
五、终止 git 命令的执行
$ git xxxx --abort
$ git merge --abort # 示例:终止merge命令的执行
最后
git 的命令还是比较多,楼主在这里仅列举了一部分,大家可以通过git --help
来查看所有命令以及命令的使用方法
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
以上是楼主的小结,好好学习不会差,我们一起进步!