【Git】Git & GitHub

  • [1. Git](#1. Git)
    • [1.1 Git基本操作](#1.1 Git基本操作)
    • [1.2 Git版本回退](#1.2 Git版本回退)
    • [1.3 Git分支操作](#1.3 Git分支操作)
  • [2. Git 配合GitHub](#2. Git 配合GitHub)
    • [2.1 生成密钥](#2.1 生成密钥)
    • [2.2 GitHub添加公钥](#2.2 GitHub添加公钥)
    • [2.3 Git连接GitHub](#2.3 Git连接GitHub)
    • [2.4 本地仓库关联远程仓库](#2.4 本地仓库关联远程仓库)
    • [2.5 本地代码push远程仓库](#2.5 本地代码push远程仓库)
    • [2.6 本地clone远程仓库](#2.6 本地clone远程仓库)
    • [2.7 本地fetch和pull](#2.7 本地fetch和pull)

1. Git

1.1 Git基本操作

bash 复制代码
touch test.py 工作区创建文件

git add test.py  提交到暂存区

git commit test.py -m "my first commit" 提交到仓库

git log 查看提交日志

git log --all 显示所有分支


git log --pretty=oneline 显示一行

git log --pretty=oneline --abbrev-commit 优化commit,前面那串数变短

git log --pretty=oneline --abbrev-commit --all --graph 显示提交合并路线

1.2 Git版本回退

bash 复制代码
版本回退

git reset --hard 要回到的版本的commitID, commitID可以用git log查看

git reflog 记录所有操作,要是reset后查不到commitID可用这个再找回。



针对某些文件不管理

touch .gitignore 创建文件

写入 *.txt  指定对.txt文件不操作

1.3 Git分支操作

bash 复制代码
git branch 查看现有分支

git branch dev01 创建新分支

git branch -d dev01 删除分支 -D强制删除

git checkout dev01 切换到dev01分支

git checkout -b dev01 有则切换无则创建



git merge dev01 把dev01合并到master上


  • 两分支更改同一文件同一行,再合并时冲突,git会在文件对应位置把分支内容都显示。后期需人工确定更改文件,再git add . git commit .

2. Git 配合GitHub

2.1 生成密钥

bash 复制代码
ssh-keygen -t rsa 生成密钥 一直回车

cat ~/.ssh/id_rsa.pub 查看公钥

2.2 GitHub添加公钥

右上角头像setting -> SSH and GPG keys 添加ssh,将公钥复制。

2.3 Git连接GitHub

ssh -T git@github.com 提示成功

2.4 本地仓库关联远程仓库

进入repository,选择ssh,如:git@github.com:Chengwen-98/git_test.git

bash 复制代码
git remote add origin(远程仓库名字) git@github.com:Chengwen-98/git_test.git
git remote 查看远程仓库

2.5 本地代码push远程仓库

git push origin master:master 分支master推给origin仓库,也叫master

git push -f origin master:master 强制覆盖

git branch -vv 看本地分支和远程分支对应关系

git push --set-upstream origin master:master 若没有对应关系,则push的时候绑定,之后push不需要。

2.6 本地clone远程仓库

bash 复制代码
git clone git@github.com:Chengwen-98/git_test.git

2.7 本地fetch和pull

git fetch origin dev01 将远程仓库的更新抓取到本地,不进行合并

git pull origin dev01 将远程仓库修改拉取到本地,进行合并(fetch+merge)

相关推荐
砖厂小工6 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮6 小时前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕7 小时前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment7 小时前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github
我叫黑大帅7 小时前
前端如何利用 GitHub Actions 自动构建并发布到 GitHub Pages?
前端·面试·github
HelloGitHub9 小时前
这个年轻的开源项目,想让每个人都能拥有自己的专业级 AI 智能体
开源·github·agent
刘发财15 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
sunny8651 天前
Claude Code 跨会话上下文恢复:从 8 次纠正到 0 次的工程实践
人工智能·开源·github
ZengLiangYi1 天前
Git Tag + Semver + CI/CD:从打标签到自动发布的完整实践
github