常用 Git Commands
连接远程仓库
创建远程仓库
- 如果是新项目,正常勾选
.gitingore
,license
,README
. - 老项目,什么都不要选,直接创建.
常用 Git Commands
# 初始化本地git仓库配置
git init
# 添加远程仓库地址
git remote add origin [ssh/https]
# 添加所有工作区代码到本地暂存区
git add .
# push 暂存区代码到本地仓库
git commit -m "[comment]"
# 改名为main,防止创建的branch叫master
git branch -m main
# 将远程仓库pull到本地
git pull origin main
git pull origin main --allow-unrelated-histories
# push代码到远程仓库
git push -u origin main
重新配置 Git
取消对项目的 git 初始化
rm -rf .git
代码版本回退
# 找到版本号回退
git reset --hard commit_id
# 强制推送到 xx 分支
git push -f origin master
创建新分支
git branch branch_name
git checkout branch_name
git checkout -b branch_name # create and checkout to branch_name
git push origin branch_name # 推送到远程仓库
删除分支
# 先退出要删除的branch
git checkout master/main
git branch -D [branch]
# 删除远程分支
git push origin --delete [branch]
修改 .gitignore
使之生效
# 清除当前缓存
git rm -r --cached .
# 重新添加文件
git add .
# 最后重新提交
git commit -m ".gitignore is now working"
Git stash
场景:当你在某一个分支上工作时,突然因为某个原因想要切换到其他分支上看看或者操作,但是你又不想提交当前分支只改了部分代码的半成品,因此,你就可以使用 git stash 来缓存当前分支的操作工作,然后再切换到其他分支操作,等操作完了,你就可以切回来将缓存的操作弹出来继续操作。当然这个缓存的操作内容不仅仅可以弹出到原来的操作分支,还可以弹出到你想要的任意分支。
保存当前的工作进度。会分别对暂存区和工作区的状态进行保存
# 查看当前所有缓存操作的堆栈记录
git stash list
stash@{0}: WIP on master: c1820a9 xxx information.
stash@{1}: WIP on master: c1820a9 yyy information.
# 查看当前某个缓存的具体缓存内容
# x为缓存列表中的数字
git stash show -p stash@{x}
# 弹出缓存操作方式一
# 如果不使用任何参数,会恢复最新保存的工作进度,并将恢复的工作进度从存储的工作进度列表中清除。
git stash pop
# 弹出缓存操作方式二
git stash apply
# 弹出指定的缓存操作,上述两种方式默认弹出缓存列表中最新的缓存记录
# 两种方式: apply & pop
git stash apply stash@{x}
git stash pop stash@{x}
# 删除某个缓存(不是弹出)
git stash drop stash@{x}
# 删除所有缓存
git stash clear
一般操作步骤
# step0: 查看当前工作分支状态,存在未跟踪的文件file_name
git status
# step1: 将操作了一半的文件进行缓存,不需要add或者commit
git stash
# step2: 再查看状态,此时提示工作分支很干净,无需任何操作,然后就可以切换到其他分支做你想做的事了
git status
git checkout other_branch_name
# ......
# step3: 其他分支操作完了,然后切回到原来分支后,重新把file_name弹出来继续操作
git stash pop
Git tag
像其他版本控制系统 (VCS) 一样,Git 可以给仓库历史中的某一个提交打上标签,以示重要。 比较有代表性的是人们会使用这个功能来标记发布结点 (
v1.0
,v2.0
,等等)
# 列出标签
git tag
# v1.0
# v2.0
# 搜索特定标签
git tag -l "v1.8.5*"
# v1.8.5
# v1.8.5-rc0
# v1.8.5-rc1
# ...
# 切换标签
git checkout <tagname>
# eg. git checkout v1.0
创建标签 tag
# 首先切换到需要打标签的分支上
git checkout [branch_name]
git tag <tagname>
# eg. git tag v1.0
# 查看标签信息和与之对应的提交信息
git show v1.0
# 查看所有tags
git tag
删除标签
# 删除掉你本地仓库上的标签
git tag -d <tagname>
# eg. git tag -d v1.0
# 删除远程标签
git push origin --delete <tagname>
Set username/email
Git 使用用户名和邮箱将 commits 与身份关联起来
# check current username/email
git config --global user.name
git config --global user.email
git config --global user.name "Lucas Hu"
git config --global user.email "<email_address>"
常见概念理解
How to understand PR is based on Branch?
- Pull requests are used to apply changes from one branches to
main/master
in order to share the work done. - It's easy to create and switch branchs.
- Using branches is more conducive to development management and code review.
git rebase
和 git merge
的区别
git rebase
- Combines two branches into a single branch with a new commit. 将两个分支合并为一个具有新提交的分支。
- Keeps all branch history. 保留所有分支历史记录。
git merge
- Moves all changes from one branch to another branch. 将一个分支的所有更改移动到另一个分支。
- Gives a clean, linear history. 提供一个干净的线性历史。
How to understand
想象一下你正在阅读一本书的两个版本(版本 A 和版本 B)。git merge 就像把两个版本的差异列在版本 A 的末尾。现在你有一个单独的章节,描述了版> 本 B 所做的改变。而 git rebase 则更像是重新写版本 A 的书,使其内容包含版本 B 的所有改变,就好像这些改变一直都在版本 A 中一样。