1. 新建+切换+链接远端分支
git checkout -b develop origin/develop
2. 解决修改Git忽略规则(.gitignore配置)不生效问题
git rm -r --cached .
git add .
git commit -m "update .gitignore"
git push origin master
3. 分支管理
查看分支:
git branch
创建分支:
git branch <name>
切换分支:
git checkout <name>
或者git switch <name>
创建+切换分支:
git checkout -b <name>
或者git switch -c <name>
合并某分支到当前分支:
git merge <name>
fast forward模式,快速合并,看不出做过合并。 不会显示 feature,只保留单条分支记录git merge <name> --no-ff
: --no-ff模式,普通合并,可以保存之前的分支历史。能够更好的查看 merge历史,以及branch 状态。会生成一个新的commit-id删除本地分支:
git branch -d <name>
删除远程分支:
git push origin -d <branch_name>
4. 标签管理
命令
git tag <tagname>
用于新建一个标签,默认为HEAD,也可以指定一个commit id命令
git tag -a <tagname> <version> -m "message..."
可以指定标签信息;命令
git tag
可以查看所有标签。命令
git tag -l xxx
可以根据xxx筛选命令
git show <tagname>
查看标签的信息命令
git tag -d <tagname>
删除标签命令
git push origin <tagname>
推送单个标签命令
git push origin --tags
推送所有标签
5. 版本回退
HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令
git reset --hard commit_id
。穿梭前,用
git log
可以查看提交历史,以便确定要回退到哪个版本。要重返未来,用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
6. 撤销管理
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- file
。场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令
git reset HEAD <file>
,就回到了场景1,第二步按场景1操作。场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考
版本回退
一节,不过前提是没有推送到远程库。
7. git stash
git stash
或者git stash save
将所有未提交的修改(工作区和暂存区)保存至堆栈中,用于后续恢复当前工作目录git stash list
查看当前stash中的内容git stash pop
将当前stash中的内容弹出,并应用到当前分支对应的工作目录上git stash apply
将堆栈中的内容应用到当前目录,不同于git stash pop,该命令不会将内容从堆栈中删除,也就说该命令能够将堆栈的内容多次应用到工作目录中,适应于多个分支的情况
堆栈中的内容并没有删除。
可以使用git stash apply + stash名字(如stash@{1})指定恢复哪个stash到当前的工作目录git stash drop <stash_name>
从堆栈中移除某个指定的stashgit stash clear
清除堆栈中的所有 内容git stash show
查看堆栈中最新保存的stash和当前目录的差异git stash show stash@{1}
查看指定的stash和当前目录差异。通过git stash show -p
查看详细的不同, 通过git stash show stash@{1} -p
查看指定的stash的差异内容git stash branch
从最新的stash创建分支
8. 配置管理
- 实用别名
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
配置别名成功后分别使用git log
与git lg
会有意向不到的效果
- 基础信息
git config --list
- 编辑信息
git config -e --global
- 编辑名称
git config --global user.name <name>
- 编辑邮箱
git config --global user.email <email>
9. 其他
- 单条记录合并
git cherry-pick <commit_id>
- commit时不校验pre-commit钩子
git commit -m '提交信息' --no-verify