1. 初始设置
git config --global user.name "Your Name"
设置用户名git config --global user.email "email@example.com"
设置Emailgit 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 lg 命令为查看历史消息git config --global credential.helper store
记住Http 连接密码git config --system --unset credential.helper store
清除已经记住的http连接密码git config --global core.quotepath fals
e 正确显示 中文字符串ssh-keygen -t rsa -C "youremail@example.com"
设置产生私钥和公钥
2. 本地命令
git init
: 创建版本管理库git log
: 查看历史信息,也可以使用如上设置的 git lggit clone xxxx
: 克隆远程仓库git tag V0.1
打标签git tag -d V0.1
删除标签git push origin --tags
推送标签到远程git status
: 查看当前库的状态git add specific_file
添加具体文件的操作到缓存区,注意,git add .
命令中使用了通配符.
,可以添加本次仓库变更的所有内容到缓存区,在使用此命令前,一定要用git status
查看当前仓库状态。git rebase -i specific_commit_hash
(-i
为"interactive", 需要用户互动选择commit,详情可百度),将所指定的commit以及所有此commit以后的commits压缩为一个commit,为让git log
历史清晰干净git clone -b xx_tag --depth=1 url
检出某个 tag 的代码,不包含其它的内容,针对大仓库会加快速度
3. 远程命令
git pull origin dev
拉去 dev 分支内容git push origin --all
推送所有分支内容到远程git push origin dev
推动 dev 分支到远程git push -u origin master -f
强制push,会使远程修改丢失,在多人开发时不建议这么操作。
4. 分支相关
- 查看分支:
git branch -a
- 创建分支:
git branch <name>
- 切换分支:
git checkout <name>
- 创建+切换分支:
git checkout -b <name>
- 合并某分支到当前分支:
git merge <name>
- 删除分支:
git branch -d <name>
- 本地分支与远程分支关联 :
git branch -u origin/dev
- 切换分支并与远程分支关联 :
git checkout -b local origin/dev
- 创建空分支:
git checkout --orphan doc
5. Merge
git config --global mergetool.meld.path "/c/Program Files (x86)/meld/meld.exe"
设定merge工具 (可选,不设置就使用默认工具)git merge dev
在当前分支,同步/合并 dev 分支的内容,并存在当前分支git checkout --theirs file.xml
解决冲突,使用远程文件git checkout --ours file.xml
解决冲突,使用本地文件
6. 子模块
git submodule init
子模块仓库初始化git submodule update --remote
子模块仓库当前分支更新git submodule add -f '+git_path+' '+file_path
添加子仓库git submodule deinit -f ' + file_path
删除子模块git rm -f ' + file_path
删除子模块
7. 仓库迁移
- 使用裸仓库指令clone下原仓库
git clone --bare xxx_a
- 在仓库中添加另外远程地址
git remote add remote2 xxx_b
- 推送仓库到另外远程地址
git push --mirror remote2
半山腰总是挤的,你得去山顶看看。 熬过无人问津的日子,才有诗和远方。