一文看 git 够了,实习干货自己的
初始化项目和上传 git Initialize the project and upload git
rm -rf .git
git init
git branch -M main
git remote add origin https://github.com/你的用户名/你的新仓库名.git
git push -u origin main / git push -u --force origin main / git push -u origin main -f
如果比如线上多了一个 MIT协议:
git pull origin main --allow-unrelated-histories --no-rebase
然后 :wq
改名方便团队人看
git config --global user.name "输入你的名字"
git config --global user.email "输入你的邮箱"
Pull
| 命令 | 适用场景 | 是否修改工作目录 | 是否自动合并 | 历史记录影响 |
|---|---|---|---|---|
git clone |
首次下载远程仓库 | 是(创建新目录) | 不适用 | 完整复制远程历史 |
git fetch |
安全获取远程更新 | 否 | 否 | 仅更新远程分支引用 |
git pull |
快速同步并合并远程变更 | 是 | 是 | 可能生成合并提交 |
pull相当于fetch后merge
fetch 这是拉下来,具体还要git pull --rebase origin feat/v2.5_0720 合并
rebase后会按照时间顺序排列好滴
merge 也要注意本地合并还是 origin/xxx 的远程合并。一般都是提交到远程操作,admin 合并
PR
提PR:先 fork 然后创建 feat 分支
然后拉下来开发
然后在别人的项目提 PR,从自己的分支-》 别人的 xx 分支
更新进度 remote add update (从 xxfork 的)
origin 是自己fork 的,这样更加方便
分支操作
git branch #查看分支
git checkout main2 # change to branch
git checkout -b my-week1 origin/week1 # 基于origin/week创建my-week1用于学习别人的代码
git branch -a #显示所有本地分支
git branch -r #显示所有远程分支
git branch -D main2 # 注意是大写的 -D ,删除
git branch -d a # 仅当 a 分支合并到当前分支时候
git push origin --delete a # 删除远程分支a
Log 看记录
# 查看提交历史
git log
git log --oneline #可以查看简化后的
remote 远程
remote storage
git remote -v #查看当前git下的remote库
git remote add origin https://github.com/your-username/myproject.git # add
# can add upstream to follow up the owner and origin is your forked repo
git pull origin main
git remote set-url origin https://casdf:g123@dev.aminer.cn/asaas/gateway.git
confirm remote
git ls-remote --heads origin
回滚
一般不会自己操作
revert
git revert HEAD # HEAD可以替换成具体commit的hash值,由于这里是最近的一次提交有bug,就直接用HEAD
- revert适合需要保留历史记录的场景!(如团队协作中已推送到远程仓库)。会保留之前的commit!!!基于之前有bug的commit重新commit放在
理解:
A—B(bug)—C—D
A—B(bug)—C—D — B‘(修改后)
reset
硬回滚:会放弃回滚目标点之后的 commit!!!
git reset --hard commit-hash(你想回到的hash)
git reset --hard HEAD~1 # 强制回到上一个commit
git push origin A --force # 让远程 A 分支也和本地一样
- 如果误删了一个提交,如何恢复?
如果用了 git reset,可以用 git reflog 查看历史操作,输出会显示所有 HEAD 的移动记录,找到误删的提交,如(c2f3a12)。git reset --hard c2f3a12
软回滚
git reset --soft 增加了456的commit-hash
将 HEAD(可以理解成你的“工作指针”) 指向 对应增加了456的commit-hash,但修改内容仍保留在工作区和暂存区,简单说就是代码内容是不变的
只回滚特定文件
git log # 查看你的commit hash
git checkout 增加了456的commit-hash -- 123.txt # 指定123.txt文件
stash 存储
方便临时存储后改到别的分支改代码,但就是说,直接做完不好么
stash 属于栈结构
用于一个工作区的没搞完需要切换的时候
git stash push -m "Save code and notes" # 同时命名 暂存区和工作区都放入
git stash drop # 默认第一个,可以带stash@{}
git stash apply 《可以带 stash@{}》 git stash pop stash@{1}
apply 不会 pop 出来,如果 pop,stash 中会删除
git stash list
git stash push --staged # 只stash暂存区的,工作区的不进入
cherry-pick
只会将将特定的commit 的文件拉过来,如果需要多个的还不如直接从特定 commit 开分支
git checkout -b feat1 <commit-hash>
rebase 变基
原始状态:
远程: A --- B --- C
本地: A --- B --- D --- E
执行 git pull --rebase origin main 后:
远程: A --- B --- C
本地: A --- B --- C --- D' --- E'
操作:
# 1. 开始变基(例如将 feature 分支变基到 main)
git checkout feature
git rebase main
# 2. 遇到冲突时,手动解决冲突文件
git add <冲突文件> # 标记冲突已解决
# 3. 继续变基(仅处理当前分支的剩余提交)
git rebase --continue
# 4. 重复步骤2-3直到所有提交应用完毕
git rebase abort # 终止rebase
merge
生成一个新的合并提交(Merge Commit),将远程分支(也可以是本地分支)和本地分支的修改合并到一起。
# 1. 切换到 main2 分支的合并目标分支(如 main)
git checkout main
# 2. 将 main2 合并到当前分支
git merge main2
# 3. 确认合并后,安全删除 main2
git branch -d main2
git merge --abort # 取消merge
如果同一个地方比如 print(123)A 分支 print(456) B 分支
在 B 上 merge A 合进来,还是保留 B 的~ (冲突另外解决,这个不算冲突)
compare with rebase
| 对比项 | git merge (合并) |
git pull --rebase (变基) |
|---|---|---|
| 冲突触发时机 | 一次性合并所有冲突(C 和 ED 的冲突一起解决) |
逐个提交解决冲突(先解决 D 和 C 的冲突,再解决 E 和 C 的冲突) |
| 解决后操作 | git add + git commit(生成合并提交) |
git add + git rebase --continue(不生成新提交) |
| 历史记录 | 保留分叉和合并提交(非线性历史) | 线性历史,本地提交哈希值改变 |
| [[this/rebase | rebase]] |
克隆大项目
历史包袱比较重的很大的包,先 clone 一层,或者同事打包发
git clone --depth=1 https://dev.aminer.cn/aico-saas/aico-rag.git
git fetch --unshallow # 后续拉过来
# 当 git fetch 拉不下来,需要本地建立远程分支引用
git fetch origin develop:refs/remotes/origin/develop
git fetch origin release:refs/remotes/origin/release
然后
git checkout -b release origin/release
git pull origin release
设置密钥给 url(有团队的密钥时)
这样方便 clone,一半 docker 中都会有设置
# Tell Go to use your Git credentials for dev.aminer.cn
git config --global url."https://wngzhin:g123C@dev.aminer.cn/".insteadOf "https://dev.aminer.cn/"
# 直接clone也可以
git clone https://wngzg:123@dev.aminer.cn/aico/base.git
git config --global --unset url."https://wngzgnin:123@dev.aminer.cn/".insteadOf # 删除~/.gitconfig 下设置条目
git config --global http.postBuffer 52428800 # 设置 git 缓冲区
# Tell Go not to use the proxy for private modules
export GOPRIVATE=dev.aminer.cn
设置 SSH
公司中https 足够了,也是登录一次
git remote add orgin git@github.com:yourname/project.git
分支提交比较
commit 对比:sourcetree / desktop等 选取多个 commit, 是“当前 commit提交后”的对比
分支对比: gitlen 的 checkbrach选择分支对比
拉取tag
公司 tag 一般是方便区分版本
git fetch --all --tags
git checkout <tag名> # v0.1.0
git checkout -b my_branch v0.1.0
更多推荐




所有评论(0)