【Git】Git常用命令总结| 8月更文挑战

253 阅读3分钟

命令

git clone - 克隆一个仓库到新的目录

# 克隆远程仓库到当前目录
$ git clone https://github.com/leetcode.git

git init - 创建一个空的Git仓库或者是对一个已经存在的Git仓库重新进行初始化

$ git init
Initialized empty Git repository in D:

git add - 添加文件到索引(支持交互模式)

# 将path目录及其子目录中所有的*.txt文件添加到索引
$ git add path/\*.txt
# 将path目录中所有的*.txt文件添加到索引
$ git add path/*.txt
# 将当前目录及其子目录中的所有文件添加到索引
$ git add .

git status - 显示工作树状态

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   subdir/sub.txt
        new file:   subdir/sub1.txt
        new file:   test.txt

git commit - 将修改记录到本地仓库

参数:
-a
--all
保存被修改和删除的文件,但是没有通过add命令通知Git的新建文件不会受到影响。
-m <msg>
--message=<msg>
使用此参数后的msg作为提交的备注。如果有多个-m指令,那它们的msg会被拼接在一起。



# 提交test.txt文件的修改记录到本地仓库,并且提交备注为msg1,msg2
$ git commit test.txt -m 'msg1' -m 'msg2'
[master (root-commit) 7345e2c] msg1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
 
# 提交所有文件的修改记录到本地仓库,提交备注为commit msg
$ git commit -am 'commit msg'

git log - 展示提交历史记录(当记录过多时,用回车键查看更多记录,使用q键退出查看

# 可以看到之前的提交记录
$ git log
commit 7345e2cf002e8ca0392ac56fd679e1725acd14da (HEAD -> master)
Author: Biubiu <374299590@qq.com>
Date:   Mon Aug 2 22:30:06 2021 +0800

    msg1

    msg2

# 只看一条历史记录
$ git log -n 1
commit 1e8a38425e4a807bd5dd1b0c5cbe8e28821fc037 (HEAD -> master)
Author: Biubiu <374299590@qq.com>
Date:   Mon Aug 2 22:37:06 2021 +0800

    update test.txt

git branch - 展示,创建或删除分支

# 创建test分支
$ git branch test

# 查看本地分支
$ git branch
* master
  test

# 查看远程分支
$ git branch -r

# 强制删除本地分支
$ git branch -D test
Deleted branch test (was 1e8a384).

# 将当前分支名改为dev
$ git branch -m dev

git checkout - 切换分支或恢复工作树文件

# 切换到master分支
$ git checkout master
Switched to branch 'master'

# 在本地新建test分支,并track远程origin仓库中的test分支
$ git checkout -b test origin/test

git merge - 合并多个提交历史

# test分支上的多个提交记录合并到当前分支
$ git merge test
  • fatal: refusing to merge unrelated histories问题解决
# 当两个分支没有关联时,直接合并会报错fatal: refusing to merge unrelated histories
# 此时需要使用以下命令
$ git merge test --allow-unrelated-histories

git stash - 将修改隐藏在一个临时的工作目录中

# 将当前的修改隐藏,并且添加备注msg
$ git stash -m 'msg'

git fetch - 拉取远程仓库最新的内容和修改

# 拉取远程origin仓库中的内容和修改
$ git fetch origin

# 拉取之前,清楚本地过时的远程分支引用
$ git fetch -p
or
$ git fetch --prune

git pull - 拉取远程仓库或本地仓库的其他分支的内容和修改,与之合并

$ git pull origin next
等价于
$ git fetch origin
$ git merge origin/next

git push - 将本地的修改提交到远程仓库,更新远程仓库中的内容

# 提交本地的修改到origin远程仓库
$ git push origin test:test

# 删除origin远程仓库的dev分支
$ git push -d origin dev

git remote - 管理追踪的远程仓库

# 添加本地仓库的远程追踪仓库,别名为origin
$ git remote add origin https://github.com/leetcode.git

# 更新本地缓存的远程分支列表
$ git remote prune origin

git cherry-pick - 合并某次提交到当前分支

# 根据某次提交的commit-id进行拉取
$ git cherry-pick 1e8a38425e4a807bd5dd1b0c5cbe8e28821fc037

git revert - 还原某些提交

# 根据某次提交的commit-id进行还原
$ git revert 7345e2cf002e8ca0392ac56fd679e1725acd14da

git config - 查询或者是仓库/全局选项

# 设置用户名、邮箱
$ git config --global user.name [username]
$ git config --global user.email [email]
  • local 只对当前仓库有效
  • global 所有仓库有效
  • system 对系统所有用户有效
# 查看配置项
$ git config -l
$ git config --list --local
$ git config --list --global
$ git config --list --system

# 清除配置
$ git config --unset --local [user.name](http://user.name)
$ git config --unset --global [user.name](http://user.name)
$ git config --unset --system [user.name](http://user.name)

git reset - 重置当前的HEAD到指定的状态

# 删除commit-id之后的所有记录,包括工作目录、暂存区、本地仓库
$ git reset --hard 7345e2cf002e8ca0392ac56fd679e1725acd14da
# 删除commit-id之后在本地仓库的记录,但会保留工作目录和暂存区的内容,并且可以在暂存区中看到本地仓库中被删除的内容
$ git reset --soft 7345e2cf002e8ca0392ac56fd679e1725acd14da
# 删除commit-id之后在暂存区和本地仓库的记录,但会保留工作目录的内容,并且可以在工作目录中看到本地仓库和本地仓库中被删除的内容
$ git reset 7345e2cf002e8ca0392ac56fd679e1725acd14da
$ git reset --mixed 7345e2cf002e8ca0392ac56fd679e1725acd14da

操作

本地已有一个代码文件,需要关联远程仓库

$ git init
$ git add .
$ git commit am "init repo"
$ git branch -M master
$ git remote add origin https://github.com/leetcode.git
$ git push origin master

本地有一些修改,但还没完成,需要去改其他内容

# 将当前工作区修改的文件进行隐藏
$ git stash -m 'create user function - undone'
# 查看隐藏列表
$ git stash list
# 等其他内容修改完成后,恢复未完成的修改,0表示最新的一个隐藏,使用pop命令后会将隐藏的修改应用到当前分支上,并且删除隐藏列表中的隐藏记录
$ git stash pop stash@{0}

参考

git官方文档