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 不知道应该从哪个远程分支拉取更新并合并到当前分支

相关推荐
fangdengfu1237 小时前
ES分析系统各个服务日志占用量
java·前端·elasticsearch
跨境数据猎手8 小时前
大数据在电商行业的应用
大数据·运维·爬虫
绿算技术9 小时前
万卡推理集群存储选型分析:从核心架构到应用视角
大数据·科技·算法·架构
兄台の请冷静10 小时前
Linux 安装es
linux·elasticsearch·jenkins
朴马丁11 小时前
预制菜的“数字厨房”:PLM如何支撑菜品标准化与供应链高效协同?
大数据·人工智能·食品行业·流程行业plm
恋喵大鲤鱼11 小时前
git mv
git·git mv
奋斗的老史12 小时前
Spring-Boot 集成 TDengine 完整实战
大数据·时序数据库·tdengine
郑洁文13 小时前
音乐数据分析研究与应用
大数据·数据挖掘·数据分析·音乐数据分析
成长之路51413 小时前
【实证分析】地市环境规制综合指数测算-原始数据+do代码(2011-2024年)
大数据
逸模14 小时前
AI+BIM 重构连锁公装新范式 逸模打造数字化营建核心底座
大数据·人工智能·笔记·其他·信息可视化·重构