git使用
常用命令
git config --global user.name "Name" // 设置用户名
git config --global user.email "Email" // 设置密码
git config --global user.name // 查看用户名
git config --global user.email // 查看密码
git init // 初始化
git clone https://github.com... // 克隆
git remote add origin https://github.com... // 设置本地用户名别名
git status // 查看状态
git add . // 将文件添加到暂存区
git commit -m "描述信息" // 将修改提交到暂存区
git log // 查看日志信息
git push origin master // 提交代码到master分支
git pull origin master // 更新master分支代码
拉取远程分支并在本地创建
git fetch origin remoteName
git checkout -b localName origin/remoteName
撤销
文件被修改,但未执行git add 操作
git checkout <fileName>
git checkout .
同时对多个文件执行了git add 操作,但只想提交其中的一部分
git add *
git status
git reset HEAD <fileName> // 取消暂存
文件执行了git add 操作,想要撤销对其的修改
git reset HEAD <fileName> // 取消暂存
git checkout <fileName> // 撤销修改
修改的文件已经被git commit,但想再次修改不再产生新的commit
git add <fileName>
git commit --amend -m "描述内容"
回滚
撤销指定文件到指定版本
git log <fileName>
git checkout <commitID> <fileName>
删除最后一次远程提交
revert
git revert HEAD
git push origin master
reset
git reset --hard HEAD^
git push origin master -f
区别:
revert:放弃指定提交的修改,但是会生成一次新的提交,需要填写提交注释,以前的历史记录都在;
reset: 将HEAD指针指导指定提交,历史记录中不会出现放弃的提交记录。
回滚某次提交
git log
git revert commitID
删除某次提交
git rebase -i "commitID"^
合并
主干合并分支
(branch) git pull // 进入分支,更新分支代码
(branch) git checkout master // 切换主干
(master) git merge branch --squash // 在主干上合并分支branch
(master) git commit -m "提交信息" // 提交合并后的代码
(master) git push // 将代码推送到远程仓库
分支合并主干
(master) git pull // 进入主干,更新主干代码
(master) git checkout branch // 切换分支
(branch) git merge master --squash // 在分支上合并主干
(branch) git commit -m "提交信息" // 提交合并后的代码
(branch) git push // 将代码推送到远程仓库
有冲突,放弃此次合并,冲突未解决
git reset --hard FETCH_HEAD
有冲突,解决了冲突,但运行失败,放弃合并
git merge --abort
创建删除分支
删除分支
git checkout master // 切换到其他分支
git branch -D <branchName> // 删除分支
创建分支
git checkout master
git pull
git checkout -b <branchName> // 创建分支
创建删除标签
创建tag
git tag <tagName> // 创建本地tag
git push origin <tagName> // 推送到远程仓库
git push origin --tags 推送多个
git tag -a <tagName> <commitId> // 以特定提交id创建tag
查看标签
git show <tagName>
git tag // 查看本地所有
git ls-remote --tags origin
删除标签
git tag -d <tagName>
git push origin --delete <tagName>
检出标签
git checkout -b <branchName> <tagName>
git checkout <tagName> // 切换标签
git flow使用
git flow init // 初始化
git flow feature start MYFEATURE // 开始新Feature:
git flow feature publish MYFEATURE // Publish一个Feature(也就是push到远程):
git flow feature pull origin MYFEATURE // 获取Publish的Feature
git flow feature finish MYFEATURE // 完成一个Feature
git flow release start RELEASE [BASE] // 开始一个Release
git flow release publish RELEASE // Publish一个Release
git flow release finish RELEASE // 发布Release,别忘了git push --tags
git flow hotfix start VERSION [BASENAME] // 开始一个Hotfix
git flow hotfix finish VERSION // 发布一个Hotfix