Git 笔记

-

常用的 Git 命令笔记.

请以官网为主. 在线学习: learngitbranching.js.org

Cues

自报家门(git教程与配置), 生成ssh(SSH配置), 关联远程(测试SSH连接)
add, commit, push

Notes

1.初始化配置

注: 以下都是陈年文章了, 这边建议直接看Cues里面的超链接.

安装完Git自报家门:

1
2
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

win下~目录生成ssh密钥:

1
ssh-keygen -t rsa -b 4096 -C "[email protected]"

如果你是团队协作开发, 请不要忘记配置Line Ending相关参数.
关联远程仓库(git默认远程名字叫origin):

1
git remote add origin SSH仓库地址

测试SSH连接:

1
ssh -T [email protected]

2.开始使用

初始化Git仓库:

1
git init

3.添加与提交

添加到缓存区(Stage):

1
git add <file>

撤销(回到最近一次add/commit):

1
git checkout <file>

提交到仓库:

1
git commit -m "提交说明"

暂存区概念图:
0

4.推送改动

推送到远程:

1
git push origin master

推送失败(远程分支比本地新),试图合并,解决冲突后再push:

1
git pull

5.版本回退

回退上个版本/回退上上个版本/回退上100个版本

1
2
3
git reset --hard HEAD^ 
git reset --hard HEAD^^
git reset --hard HEAD~100

通过commit id回退

1
git reset --hard commit_id

6.查看版本log

参数为简化信息,commit id由SHA1计算出来

1
git log --pretty=oneline

后悔药,记录了你每一次git操作

1
git reflog

7.分支操作

查看当前分支

1
git branch

创建dev分支

1
git branch dev

切换到dev分支:

1
git checkout dev

创建并切换分支:

1
git checkout -b dev

删除本地dev分支:

1
git branch -d dev

删除远程dev分支(分之前冒号代表删除):

1
git push origin :dev 

合并分支(当前为master,合并dev分支到master)

1
git merge dev

禁用fast forward的合并:

1
git merge --no-ff -m "merge with no-ff" dev

分支合并情况:

1
git log --graph --pretty=oneline --abbrev-commit

推送分支:

1
git push origin <branch>

8.标签操作

查看所有标签:

1
git tag

查看标签信息:

1
git show <tag name>

打标签

1
git tag <name>

指定commit id打标签:

1
git tag <tag name> <commit id>

删除标签

1
git tag -d <name>

推送标签到远程:

1
git push origin <tag name>

删除一个远程标签:

1
git push origin :refs/tags/<tag name>

9.其他操作

关联多个远程:
关联github远程库:

1
git remote add github [email protected]:michaelliao/learngit.git

关联码云远程库:

1
git remote add gitee [email protected]:liaoxuefeng/learngit.git

移除关联:

1
git remote remove {REMOVE_NAME}

Summary

git上手难度还是比较高的, 只有理解和多练习了, 才能熟练掌握git.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(*main) git merge bugFix
(*bugFix) git rebase main
git branch -f main C1
git checkout HEAD^
(HEAD回退到第2个父级)git checkout main^2
(HEAD回退3次提交) git checkout HEAD~3
(HEAD回退1, 回退第2个父级, 回退3) git checkout HEAD~^2~3
(本地回溯) git reset HEAD~1
(创建和本次提交相反的新提交. 远程共享, 可 push) git revert HEAD
git cherry-pick C3 C4 C7
(交互式变基) git rebase -i HEAD~4
(重写 commit) git commit --amend
git tag v1 C1
git describe main
git rebase --onto <newbase> <oldbase> <branch>
(克隆远程分支到本地) git clone
git checkout origin/main
(获取远程提交到本地的 origin/* 分支, 不会改变本地的分支) git fetch
git fetch; git merge origin/main = git pull
git fetch; git rebase origin/main = git pull --rebase
git fakeTeamwork
git fakeTeamwork foo 3
git push
git fetch; git rebase origin/main; git push
git fetch; git merge origin/main; git push
git checkout -b foo o/main; git pull
git branch -u o/main foo
git push origin main
git push origin foo^:main
git fetch origin foo
git fetch origin C2:bar

参考资料:
Git教程-廖雪峰的官方网站
git使用简易指南
https://gitimmersion.com/lab_01.html
Connecting to GitHub with SSH
测试SSH连接