Git

Model for Git

type blob(file) = array<byte>;
type tree(folder) = map<string, tree | blob>; # mapping the string(folder names) of a tree to blob or another tree that may contain blobs.
type commit = struct{
parents: array<commit>(pointer);
author: string(pointer);
message: string(pointer);
snapshot: tree(pointer);
}
type object = tree|blob|commit;
# Git maintain a series of object named objects on disk through hash mapping;
objects = map<string, object>
# We can retrive a certain object stored on disk through his hash(SHA-1) value(string)(uid).

SHA-1 is a hexadecimal strings that are 40 characters long(namely 160bits)

references = map<string(SHA-1),string(human-readable names)>

In reality Git only mainly maintains two pieces of data—objects and references.

Command For Git

git init
git clone https://github.com/octocat/Spoon-Knife.git
git status
git add
git commit -m "description"
git commit -p
git log
git log --oneline
git log --all --graph --decorate
git branch
git branch newBranch
git checkout endBranch
# tell me about all the branches that I know about in a very verbose way
git branch -vv(very verbose)

# 将指定分支的更改合并到当前所在的分支
git merge endBranch
# 除本地已合并的分支。如果分支未合并,需要使用 -D 强制删除
git branch -d endBranch

# add remote repo
git remote add <remote repo name> <remote repo URL>

# 查看当前仓库关联的远程仓库地址
git remote -v

# 命令删除原有 origin 的通信方式
git remote rm origin

#
git clone <url> <local path of destinated folder>

# git push [远程仓库名] [分支名]
# 将本地的提交推送到远程仓库。通常远程仓库名为 origin。
# 首次推送时可能需要设置上游: git push -u origin main
git push <remote repo name> <local branch>:<remote branch>

# git pull [远程仓库名] [分支名]
# 从远程仓库拉取最新的更改到本地
git pull origin main

# 设置用户名
git config --system/--global/--local user.name "你的名字"
# 设置用户邮箱
git config --system/--global/--local user.email "你的名字"
# 查看特定级别或所有级别的配置
# 查看所有配置(包括所有级别)
git config --list
# 查看全局配置
git config --global --list
# 查看本地配置
git config --local --list
# 查看特定配置项的值
git config user.name
# 删除特定的配置项
git config --unset --global user.name

# 为常用的 Git 命令设置更短、更方便的别名。
# 将 git status 设为 st:
git config --global alias.st status
# 现在可以用 git st 代替 git status。

# checkout specific info with hash value within a object
git cat-file -p <hashValue>

# see changes inside a object
git diff [<hash value>] <blob> # the command will check HEAD by default if without <hash value>.
git diff HEAD <blob>
git diff <the hash of object compared to HEAD> HEAD <blob>

#
git merge <branch>
git merge --abort
git merge --continue
git stash
git stash pop
git bisect

在用了 git push -u origin main 后我如何让本地的 main 分支跟踪 origin/master 分支?

你之前用 git push -u origin main 将本地 main 分支和远程 origin/main 关联起来了。现在你想让本地的 main 分支转而跟踪 origin/master 分支。

要做到这一点,你可以使用 git branch 命令的 --set-upstream-to 选项。


更改本地分支的跟踪关系

  1. 首先,确保你的本地 main 分支是最新的。

    这很重要,因为如果你要跟踪 origin/master,你应该先确保 origin/master 的最新状态已经在你的本地。

    Bash

    git fetch origin

    这条命令会从 origin 远程仓库下载所有新的分支和提交信息,但不会自动合并到你当前的分支。

  2. 切换到 main 分支(如果不在的话):

    Bash

    git checkout main
  3. 设置本地 main 分支跟踪 origin/master

    Bash

    git branch --set-upstream-to=origin/master main
    • git branch: Git 分支管理命令。
    • --set-upstream-to=origin/master: 指定要将当前分支(或指定的分支)的上游设置为 origin 远程仓库的 master 分支。
    • main: 这是你希望设置跟踪关系的本地分支名称。

    执行此命令后,你会看到类似这样的输出:

    Branch 'main' set up to track remote branch 'master' from 'origin'.

确认更改

你可以使用 git statusgit branch -vv 来验证新的跟踪关系:

  • git status

    现在当你运行 git status 时,Git 会告诉你你的本地 main 分支是领先或落后于 origin/master 的。

    例如:

    Bash

    On branch main
    Your branch is up to date with 'origin/master'.
    nothing to commit, working tree clean

    或者

    Bash

    On branch main
    Your branch is ahead of 'origin/master' by 1 commit.
    (use "git push" to publish your local commits)
    nothing to commit, working tree clean
  • git branch -vv

    这个命令会显示所有本地分支及其跟踪的远程分支信息。

    你会看到类似这样的输出:

    * main        <commit_hash> [origin/master] <commit_message>
    another-branch <commit_hash> [origin/another-branch] <commit_message>

    这里的 [origin/master] 明确表示你的本地 main 分支现在正在跟踪 origin/master


注意事项

  • 同步内容: 仅仅更改跟踪关系并不会自动合并 origin/master 的内容到你的 main 分支。如果你想把 origin/master 的最新代码同步到你的本地 main 分支,你需要执行 git pull(在设置了新的跟踪关系后)。

    Bash

    git pull

    由于你已经将 main 跟踪到 origin/master,现在 git pull 会自动从 origin/master 拉取并合并到本地 main

  • 分支名称: 在许多新项目中,main 已经是默认的主分支名称。如果你选择让 main 跟踪 master,请确保你理解这样做的原因和潜在的影响,比如在 Git 托管平台(如 Gitee)上,你的 Pull Request 默认目标分支可能仍是 master,这可能会导致一些混乱。通常建议本地主分支和远程主分支保持一致。

git pull:拉取并合并

  • 作用: git pull 是一个组合命令,它实际上等于执行了两个步骤:

    1. git fetch:从远程仓库下载最新的更改。
    2. git merge(默认):将这些下载下来的更改自动合并到你当前所在的本地分支。
    • 你也可以配置 git pull 默认执行 git rebase 而不是 git merge (通过 git config pull.rebase true)。
  • 影响工作区: git pull 会立即更新你的本地工作区,将远程的更改反映到你正在编辑的文件中。

核心区别总结:

特性 git fetch git pull
操作 下载远程更改到本地仓库 下载远程更改并自动合并到当前本地分支
影响 只更新本地的远程跟踪分支,不影响工作区 更新工作区,改变本地文件
组成 单一操作 git fetch + git merge (或 git rebase)
何时用 查看远程更新,但不立即合并 确定要同步并应用远程更新
JerryYu@DESKTOP-HGF21VE MINGW64 /d/CodeBuffer/Gitee (main)
$ git commit -m "1"
[main (root-commit) ce7a74a] 1
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

JerryYu@DESKTOP-HGF21VE MINGW64 /d/CodeBuffer/Gitee (main)
$ git cat-file -p ce7a74aJ
fatal: Not a valid object name ce7a74aJ

JerryYu@DESKTOP-HGF21VE MINGW64 /d/CodeBuffer/Gitee (main)
$ git cat-file -p ce7a74a
tree 68aba62e560c0ebc3396e8ae9335232cd93a3f60
author 寒酥梦 <15699295+cold-crispy-dream@user.noreply.gitee.com> 17508548
99 +0800
committer 寒酥梦 <15699295+cold-crispy-dream@user.noreply.gitee.com> 17508
54899 +0800

1

# git cat-file -p ce7a74a(the hash of certain commit) can check info inside one commit