git常见命令

git cli

号表示注释,asterisk(*)星号表示可选,curly brace{}大括号表示参数

配置用户信息

git config --global user.name {your name} #to tell git who you are
git config --global user.email {your email} #to tell git what‘s your email

git提交与状态

git status #display status of current git
git add {filename} #add the file which hasn't been commit, a point(.) denotes all,git add .
git commit -m {some detail about this commit} # submit the revision and comment

git日志

git log #display detailed information about history submission records
git log --pretty=oneline #show information by one line
git log --abrev-commit #only display the first few digits of hash value
git log --all #show all of commits information
git log --graph #reflect the submission relation intuitively

题外话,alias

Oh on,it's difficult to remember!!! just use alias to abbrev code's length;

alias ll="ls -al" #use ll instead of "ls -al"

回退提交

git reset --hard {commitID} #reset to the specify version;such as "git reset --hard a3b4efd"
git reflog #view historical operational information

.gitignore

How to ignore the file which you don't want to manage? Add wildcards to ".gitignore" file to ignore which file you don't want manage;

分支

Branch

git branch #view all branch
git branch {branch_name} #create branch
git checkout {branch_name} #switch to specify branch
git checkout -b {branch_name} #create and siwtch to the branch
git merge A #merge A to current branch

git branch -d {branch_name} #delete branch
git branch -D {branch_name} #force delete

密钥

ssh -keygen -t rsa              #create public key of ssh
ssh -T git@gitee.com       #test connecting
cat ~/.ssh/id_rsa.pub       #browse public key

远程仓库

git remote add {nickname} {your remote repository}      #add remote repository
git push {remote repository nickname} {local branch}    #push local branch to remote repository
git push {remote repository nickname} {local branch}:{remote branch}       #Push local branch to remote branch,if local name is same of remote's ,you can omit the remote branch;
git push --set-upstream {remote repository nickname} {local branch}:{remote branch} #band local branch to remote branch,after this,you can use "git push" to intead of that;
git branch -vv #check the banding relationship

获取远程仓库

git clone {remote repository addres(https/ssh)} {*nick name} #get a remote repository,asterisk(*) denotes an optional item
git fetch {remote repository name} {remote branch name}      #fetch a remote branch,but not merge;
git fetch #same with "git fetch origin,fetch all branch of origin
git pull {remote repository nickname} {remote branch} #fetch remote branch and merge to current branch;

消除混淆

  • 在 Git 中,不能直接修改远程跟踪分支(如 origin/main)的内容,因为它们是本地对远程分支状态的只读引用,仅用于跟踪远程仓库的更新。若需修改代码,必须通过本地分支操作,再将更改推送到远程。
  1. git fetch 的核心作用
    git fetch 会更新远程跟踪分支(remote-tracking branches),但不会直接修改本地分支(local branches)或工作目录(working directory)。
    远程跟踪分支:存储在 .git/refs/remotes/ 目录下,是本地对远程分支的“镜像”(如 origin/main 对应远程仓库的 main 分支)。

    本地分支:存储在 .git/refs/heads/ 目录下,是你直接工作的分支(如 main、develop)。
    工作目录:包含你当前修改的文件(未提交的更改)。

  2. git fetch 是否拉取所有历史版本?
    不是拉取所有历史版本,而是拉取远程分支的最新提交(即最新的提交对象及其祖先链)。
    Git 会下载远程分支的最新提交(包括提交记录、文件变更等),但不会自动下载整个仓库的所有历史(除非显式请求或首次克隆)。
    远程仓库的完整历史存储在 .git/objects 目录中,但 git fetch 默认只下载当前分支需要的最新数据(基于引用更新)。
    例外情况:
    如果是首次克隆后的第一次 fetch,Git 会下载远程仓库的所有分支的最新提交(但不会自动创建本地分支)。
    如果使用 git fetch --all,会拉取所有远程分支的最新提交(但不会合并或创建本地分支)。
    如果使用 git fetch --depth=N(浅克隆),则只拉取最近的 N 次提交(不包含完整历史)。

  3. git fetch 不填参数时的默认行为
    默认拉取 origin 远程仓库的所有分支的最新提交,但不会自动更新本地分支。

git pull 不填参数时,不会拉取所有远程分支,而是仅拉取当前分支对应的远程跟踪分支的更新,并尝试合并到当前分支。 如果当前分支没有关联远程分支(upstream),直接运行 git pull 会报错,因为 Git 不知道应该从哪个远程分支拉取更新并合并到当前分支

posted on 2026-01-29 18:10  蟒蛇配咖啡  阅读(0)  评论(0)    收藏  举报

导航