Git

发布时间 2023-11-04 13:49:45作者: liuzonglin

Git 常用命令速查表

Git 全局设置:

$ git config --global user.name "user_name"      # 设置用户签名
$ git config --global user.email "user_email"     # 设置用户邮箱

说明:

签名的作用是区分不同操作者身份。

用户的签名信息在每一个版本的提交信息中能够看 到,以此确认本次提交是谁做的。

Git 首次安装必须设置一下用户签名,否则无法提交代码。

注意:
这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任 何关系。

~> open C:\Users\liuzonglin\.gitconfig
[user]
        name = user_name
        email = user_email
[http]
        sslVerify = false
[core]
        symlinks = true
[credential "https://code.aliyun.com"]
        provider = generic

创建 git 仓库:

mkdir <repository_name>
cd <repository_name>
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin <remote_repository_address>
git push -u origin "main"

已有仓库?

cd <repository_name>
git remote add origin <remote_repository_address>
git push -u origin "main"

创建版本库

$ git clone <remote_repository_address> # 克隆远程版本库

$ git init # 初始化本地版本库
Initialized empty Git repository in D:/.github/.dome/df/.git/

$ ls -al
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 ./
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 ../
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 .git/ # 生成了 `.git`

$ cat .git/config # 每个git库都会有一个配置信息文件
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = https://gitee.com/liuzonglin1/df.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main

修改和提交

$ git status                            # 查看本地库状态

$ git diff                              # 查看变更内容

$ git add <file>                        # 跟踪指定的文件

$ git add .                             # 跟踪所有改动过的文件

$ git mv <old> <new>                    # 文件改名

$ git rm <file>                         # 删除文件

$ git rm --cached <file>                # 停止跟踪文件但不删除

$ git commit -m "日志信息"               # 提交所有更新过的文件

$ git commit -m "日志信息" <文件名>       # 将暂存区的文件提交到本地库

$ git commit --amend                   # 修改最后一次提交

查看提交历史记录

$ git log               # 查看提交历史

$ git log -p <file>     # 查看指定文件的提交历史

$ git blame <file>      # 以列表方式查看指定文件的提交历史

$ git reflog            # 列出引用日志的条目
aa9b0e2 (HEAD -> main, origin/main) HEAD@{0}: commit (merge): 合并所有差异
501fa20 HEAD@{1}: checkout: moving from main-old2 to main
22f7c73 HEAD@{2}: checkout: moving from main-old to main-old2
e19c344 HEAD@{3}: checkout: moving from main to main-old
501fa20 HEAD@{4}: checkout: moving from main-old to main

撤销

$ git reset --hard HEAD         # 撤销工作目录中所有未提交文件的修改内容

$ git checkout HEAD <file>      # 撤销指定的未提交文件的修改内容

$ git revert <commit>           # 撤销指定的提交

Git 分支与标签

$ git branch                    # 显示所有本地分支
* main 5e476cb [ahead 1] 2      # * 代表当前所在的分区

$ git branch <new_branch>       # 创建新分支

$ git checkout <branch_name/tag> # 切换到指定分支标签

$ git branch -d <branch_name>   # 删除本地分支

$ git tag                       # 列出所有本地标签

$ git tag <tag_name>            # 基于最新提交创建标签

$ git tag -d <tag_name>         # 删除标签

合并与衍合

$ git merge <branch_name>  # 合并指定分支到当前分支

$ git rebase <branch_name> # 衍和指定分支到当前分支

$ cat ss.txt # 查看冲突文件
<<<<<<< HEAD # 特殊符号
111111111111111111111111111111111111111111111111111111111111111111111111
======= # 当前分支的代码
123456
>>>>>>> lzl # 合并过来的代码
$ vim ss.txt # 编辑有冲突的文件,删除特殊符号,决定要使用的内容

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (main|MERGING)
$ cat ss.txt
111111111111111111111111111111111111111111111111111111111111111111111111
123456

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (main|MERGING)
$ git add ss.txt # 添加到暂存区

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (main|MERGING)
$ git commit -m "解决冲突" # 执行提交(注意:此时使用 git commit 命令时不能带文件名)
[main 3eaeba5] 解决冲突

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (main) # MERGING 消失,变为正常

Git 远程仓库操作

$ git remote -v                 # 查看远程版本库信息

$ git remote show <remote_name> # 查看指定远程版本库信息

$ git remote add <remote_name> <url> # 添加远程版本库

$ git fetch <remote_name>            # 从远程库获取代码

$ git pull <remote_name> <branch_name> # 下载代码及快速合并

$ git push <remote_name> <branch_name> # 上传代码及快速合并

$ git push origin --delete branch_name # 删除远程分支或标签

$ git push --tags # 上传所有标签
  • 其中,origin是远程仓库的名称,branch_name是要删除的分支的名称。

强制覆盖本地文件

git pull 强制覆盖本地的代码方式,下面是正确的方法:

git fetch --all

然后,你有两个选择:

git reset --hard origin/main

或者如果你在其他分支上:

git reset --hard origin/<branch_name>

说明:

git fetch 从远程下载最新的,而不尝试合并或 rebase 任何东西。

然后 git reset 将主分支重置为您刚刚获取的内容。 --hard 选项更改工作树中的所有文件以匹配origin/main中的文件。

git 切换 Commit

git switch -c <new_branch_name>
  • 如果您想要创建一个新分支以保留您创建的提交,您可以使用 -c 开关与切换命令一起使用。

参考文档: