《命令行》Git

作者: 敏捷Studio | 来源:发表于2018-06-25 16:08 被阅读4次
Git 常用命令

创建版本库

git clone 克隆远程版本库

git clone xxx.git 克隆文件
git clone xxx.git <dir> 克隆文件到dir文件目录下
git clone -b [new_branch_name]  xxx.git 克隆并切换到新分支上

git init 初始化本地版本库

修改和提交

git status 查看修改状态
git diff 查看变更内容

git diff 比较当前所有文件和暂存区文件差异
git diff <file> 比较当前特定文件和暂存区文件差异
git diff <commit-id1> <commit-id2> 比较两次提交差异
git diff <branch1> <branch2> 比较两个分支差异
git diff --staged 比较暂存区和版本库差异
git diff --cached 比较暂存区和版本库差异
git diff --stat 仅仅比较统计信息

git show 显示最后一次 commit 修改的内容

git show commit-id 显示某次 commit-id 的修改

git add 保存文件到暂存区

git add <file>   保存指定的文件
git add .  保存所有修改过的文件
git add -u   保存修改和删除,但是不包括新建文件

git mv <old> <new> 文件改名
git rm

git rm <file> 从版本库中删除文件
git rm --cached <file> 从版本库中删除文件,但不删除文件

git commit

git commit –m “comment” 提交所有更新过的文件
git commit --amend 修改最后一次提交

查看提交历史

git log 查看提交历史

git log -p 查看最近 p 次提交
git log -p -n 查看最近 n 次详细修改内容的 diff
git log <file> 查看指定文件的提交历史
git log --author=”author”  查看某人提交记录
git blame <file> 以列表方式查看指定文件的提交历史

撤销

git checkout 用于切换分支或恢复工作树文件

git checkout <file> 撤销指定的未提交文件的修改内容
git checkout <branch> 切换分支

git reset

git reset --mixed   取消 commit + add
git reset --soft      取消 commit
git reset --hard     取消 commit + add + local working
git reset --hard HEAD 撤销工作目录中所有未提交文件的修改内容
git reset <file>  从暂存区恢复到工作文件
git reset -- . 从暂存区恢复到工作文件

git revert

git revert HEAD      撤销最近的一个提交
git revert <commit> 撤销指定的提交

分支与标签

git branch 分支

git branch -a  查看所有分支 
git branch <new-branch> 创建本地新分支
git branch -b <new-branch> 创建本地分支并切换到此分支
git branch -d <branch> 删除本地分支
git branch -m <src-branch> <dest-branch> 本地分支重命名

git tag 列出所有本地标签

git tag <tag_name> 基于最新提交创建标签
git tag -d <tag_name> 删除标签

合并与衍和

git merge 合并指定分支到当前分支

git merge <branch> 不显示 feature,只保留单条分支记录
git merge <branch> --no-ff 保存你之前的分支历史
git rebase <branch> 衍合指定分支到当前分支

远程操作

git remote 列出所有 remote 的别名

git remote -v 查看远程版本库信息,列出 url
git remote show <remote> 查看指定远程版本库信息
git remote rm [name] 删除一个 remote
git remote rename [old-name] [new-name] 重命名 remote
git remote add <remote> <url> 添加远程版本库

git fetch 从远程获取代码

git fetch <remote> 从远程获取代码

git pull

git pull <remote> <branch> 下载代码及快速合并
pull = fetch + merge

git push

git push <remote> <branch> 上传代码及快速合并
git push <remote> : <branch/tag-name> 删除远程分支或标签
git push --tags 上传所有标签
git push origin branch_name 将本地分支同步到远端
git push origin :branch_name 删除远程分支,慎重使用

帮助

git help 帮助

git help <command> 显示 command 的 help

实战

撤销一个add
reset HEAD . 撤销所有的文件
git reset HEAD -filename 撤销某个文件

撤销一个commit(未PUSH)
git log -5 (找到你要撤销的commit)
git reset --hard commitid(你要撤销的上一个commit_id)

撤销一个commit (已经PUSH)
将 A 分支上的若干 commit-id 提交到 B 分支上
git log:A 分支上找到若干 commit-id
git checkout B :切换到 B 分支
git cherry-pick <commit-id> :pick 到 B 分支上
git commit -c <commit-id> :若有冲突 则commit -c
git push B :若无冲突则直接push
Git 技术栈

未完待续。。。持续更新。。。

相关文章

网友评论

    本文标题:《命令行》Git

    本文链接:https://www.haomeiwen.com/subject/znsjkftx.html