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

相关推荐
蚁巡信息巡查系统9 小时前
网站信息发布再巡查机制怎么建立?
大数据·人工智能·数据挖掘·内容运营
云边云科技_云网融合9 小时前
AIoT智能物联网平台:架构解析与边缘应用新图景
大数据·网络·人工智能·安全
黑屋里的马9 小时前
GitExtension下载、安装
git·gitextension
Geoking.10 小时前
Git 中的 Rebase 与 Merge:原理、区别与最佳实践
git
青岛前景互联信息技术有限公司10 小时前
政策支撑:应急部推动化工园区安全风险智能化管控平台有效应用!
大数据·人工智能·安全
才盛智能科技10 小时前
歪麦霸王餐&元K(才盛云)签订战略合作
大数据·人工智能·物联网·自助ktv系统·才盛云
峥嵘life10 小时前
Android EDLA CTS、GTS等各项测试命令汇总
android·学习·elasticsearch
WZgold14110 小时前
黄金突然跳水!是技术调整还是趋势反转?
大数据·经验分享
开源能源管理系统10 小时前
开源筑基,智领零碳:MyEMS 赋能零碳工厂全周期转型新实践
大数据·开源·能源·能源管理系统·零碳工厂
金融小师妹11 小时前
AI算法与多因子模型驱动:金价获利了结涌现后的高位下跌态势
大数据·人工智能·深度学习·机器学习