Git常用命令归档

来源:互联网 发布:怎么关掉淘宝店铺 编辑:程序博客网 时间:2024/06/08 02:37

代码操作命令

# clone远程代码$ git clone https://github.com/HuaweiBigData/StreamCQL# 更新本地代码到最新$ git pull# 当前状态查看$ git status# 日志查看$ git log# 添加修改代码到暂存区$ git add fileName$ git add # all# 将代码从暂存区移除$ git reset HEAD fileName# 回退所有未提交的修改$ git checkout #  fileName# 本地提交代码$ git commit -m "comments"# 推送修改到远程dev分支$ git push origin dev

分支管理

# 查看当前分支$ git branch # 查看本地已经同步的远程分支$ git branch -r# 查看所有分支$ git branch -a# 在本地创建dev分支$ git branch dev# 推送本地分支到远程$ git push origin dev#  切换代码到指定分支$ git checkout dev# 删除远程分支$ git push origin --delete dev# 删除本地分支$ git branch --delete dev# 同步远程和本地分支$ git remote prune origin

远程操作

# 列出所有的远程主机$ git remote# 查看所有远程主机的地址$ git remote -v# 添加远程主机$ git remote add upstream https://github.com/HuaweiBigData/StreamCQL.git# 删除远程主机$ git remote rm upstream# 修改远程主机名称$ git remote rename upstream newname# 取回远程版本库所有更新$ git fetch upstream# 取回远程版本库master分支更新$ git fetch upstream master# 合并远程master分支到本地master$ git merge upstream/master# 切换到远程分支,并在本地创建分支$ git checkout -b upmaster upstream/master

标签

# 列出所有tag$ git tag# 新建一个tag在当前commit$ git tag [tag]# 新建一个tag在指定commit$ git tag [tag] [commit]# 删除本地tag$ git tag -d [tag]# 删除远程tag$ git push origin :refs/tags/[tagName]# 查看tag信息$ git show [tag]# 提交指定tag$ git push [remote] [tag]# 提交所有tag$ git push [remote] --tags# 新建一个分支,指向某个tag$ git checkout -b [branch] [tag]

查看

# 显示有变更的文件$ git status# 显示当前分支的版本历史$ git log# 显示commit历史,以及每次commit发生变更的文件$ git log --stat# 显示某个文件的版本历史,包括文件改名$ git log --follow [file]$ git whatchanged [file]# 显示指定文件相关的每一次diff$ git log -p [file]# 显示指定文件是什么人在什么时间修改过$ git blame [file]# 显示暂存区和工作区的差异$ git diff# 显示暂存区和上一个commit的差异$ git diff --cached [file]# 显示工作区与当前分支最新commit之间的差异$ git diff HEAD# 显示两次提交之间的差异$ git diff [first-branch]...[second-branch]# 显示某次提交的元数据和内容变化$ git show [commit]# 显示某次提交发生变化的文件$ git show --name-only [commit]# 显示某次提交时,某个文件的内容$ git show [commit]:[filename]# 显示当前分支的最近几次提交$ git reflog

重置

# 恢复暂存区的指定文件到工作区$ git checkout [file]# 恢复某个commit的指定文件到工作区$ git checkout [commit] [file]# 恢复上一个commit的所有文件到工作区$ git checkout .# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变$ git reset [file]# 重置暂存区与工作区,与上一次commit保持一致$ git reset --hard# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变$ git reset [commit]# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致$ git reset --hard [commit]# 重置当前HEAD为指定commit,但保持暂存区和工作区不变$ git reset --keep [commit]# 新建一个commit,用来撤销指定commit,后者的所有变化都将被前者抵消,并且应用到当前分支$ git revert [commit]

打包

# 生成一个可供发布的压缩包$ git archive

说明:
部分命令引用自:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

1 0