菜鸟教程
图形化git学习
官方参考教程
警告
每次修改文件之前确保目前处在某一分支上,不然工作全白做!
本地仓库
全局修改默认分支名称
1
| git config --global init.defaultBranch main
|
初始化
1 2 3 4 5 6 7 8 9
| git init git add . git commit -m "提交信息"
git commit --allow-empty-message -m " "
git commit -a -m "提交信息"
git commit -a --allow-empty-message -m " "
|
分支管理
创建并切换到新分支
1
| git checkout -b <newbranch>
|
查看分支
1 2 3 4 5 6
| git branch
git branch -r
git branch -a
|
删除分支
需切换到其他分支
1 2 3 4 5 6
| git branch -d <branch>
git branch -D <branch>
git push origin :<branch>
|
其他分支管理命令
1 2 3 4 5 6 7 8 9
| git merge origin/main git rebase
git rebase -i <commit> git cherry-pick git branch git checkout git switch ......
|
暂存与取出
1 2 3 4
| git stash git stash pop
git stash clear
|
撤销与删除
git reset
更改当前分支的提交历史,重置当前分支到特定提交
1 2 3 4 5 6
| git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>
|
git revert
创建一个新的提交,用来撤销指定的提交,它不会改变提交历史,适用于已经推送到远程仓库的提交
1 2
| git revert <commit> git revert HEAD
|
图形化命令
1 2 3 4 5
| git log --graph git log --oneline --graph
gitk
|
图形化窗口乱码解决方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ># 1. 设置提交日志的编码为UTF-8 >git config --global i18n.commitencoding utf-8
># 2. 设置日志输出、界面展示的编码为UTF-8 >git config --global i18n.logoutputencoding utf-8
># 3. 设置Git核心文件编码为UTF-8 >git config --global core.encoding utf-8
># 4. 关闭文件名转义,解决中文文件名乱码 >git config --global core.quotepath false
># 5. 针对Git GUI/gitk设置界面编码 >git config --global gui.encoding utf-8
|
忽略特定文件
- 创建
.gitignore文件
- 编写忽略规则
每行写一个忽略规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| # 忽略单个文件 secret.txt
# 忽略整个目录 node_modules/ dist/
# 忽略特定类型的文件 *.log *.tmp
# 忽略目录下的特定文件 build/*.log
# 不忽略某个文件(即使前面规则匹配了) !important.log
|
- 提交
.gitignore文件
1 2
| git add .gitignore git commit -m "Add .gitignore file"
|
- 处理已被 Git 跟踪的文件
如果文件已经被 Git 跟踪,需要先从 Git 缓存中移除
1 2 3 4 5 6 7 8
| git rm --cached secret.txt
git rm -r --cached node_modules/
git commit -m "Stop tracking ignored files"
|
打包本地仓库
1 2 3 4 5 6
| git bundle create repo.bundle --all
git clone repo.bundle
git clone --mirror
|
其他本地命令
1 2 3 4 5 6 7 8 9 10 11 12
| git commit --amend -m "This is the correct message"
git branch -m master master_copy
git diff
git status
git submodule
git tag
|
远程仓库
推送
1 2 3 4 5 6 7 8 9 10
| git remote add origin git@jihulab.com:jianyuewushuang/technicaldocumentation.git
git remote set-url origin https://jihulab.com/jianyuewushuang/technicaldocumentation.git
git remote add origin https://jihulab.com/jianyuewushuang/technicaldocumentation.git
git push -u origin main
git branch --set-upstream-to=origin/main main
|
拉取
1 2 3
| git pull origin main --rebase
git pull origin main --allow-unrelated-histories
|
拉取失败时:
git stash暂存未提交的修改git stash clear清楚暂存的修改
git clean -f删除未跟踪的文件
断开与远程仓库的连接
1
| git remote remove origin
|
克隆
1 2 3 4 5
| git clone <远程仓库地址>
git clone --depth 1 <远程仓库地址>
git fetch --unshallow
|
调整缓冲区大小
1 2
| git config --global http.postBuffer 524288000
|
图形化git工具
- Git GUI
- Sourcetree
- Git Extensions
- GitKraken(界面友好)
- Tower
- GitHub Desktop
- TortoiseGit(右键操作)
- GitButler(生成并在软件里管理虚拟分支)
- Gitnuro
- Git Cola
- sourcegit(界面友好,中文支持)