Git常用命令


专用名词

Workspace:工作区
Index / Stage:暂存区
Repository:仓库区(或本地仓库)
Remote:远程仓库

初始化

git init                初始化本地库

git init [project-name]                新建一个目录,将其初始化为Git代码库


设置签名:区分不同开发人员的身份

项目级别/仓库级别:仅在当前本地库范围内有效

(信息保存位置:.git/config文件)

git config user.name Solin_pro

git config user.email Solin_pro@svip.com

系统用户级别:登录当前操作系统的用户范围

(信息保存位置:~/.gitconfig文件)

git config --global user.name Solin_glb

git config --global user.email Solin_glb@svip.com


添加、提交、删除文件

git help [command]                显示command的help

git add [file]                将工作区文件修改提交到本地暂存区

git add .                添加当前目录的所有文件到暂存区

git rm [file]                删除工作区文件,并且将这次删除放入暂存区

git rm --cached [file]                 删除暂存区的文件, 但工作区的文件不会被删除

git mv [file-original] [file-renamed]                修改文件名称,并且将这个改名文件放入暂存区

git commit -m [message]                提交暂存区到仓库区

git commit [file1] [file2] ... -m [message]                提交暂存区的指定文件到仓库区

git commit -a -m [message]                提交工作区自上次commit之后的变化,直接到仓库区

git commit -v                提交时显示所有diff信息


查看信息

git status                查看工作区、暂存区状态

git log                显示当前分支的版本历史

git log --stat                显示commit历史,以及每次commit发生变更的文件

git log [file]                 查看该文件每次提交记录

git log -p [file]                显示指定文件相关的每一次详细修改内容的diff

git log -p -2                查看最近两次详细修改内容的diff

git log -5 --pretty --oneline                显示过去5次提交

git reflog                显示当前分支的最近几次提交

git shortlog -sn                显示所有提交过的用户,按提交次数排序

git diff [file]                显示(指定文件在)暂存区和工作区的差异

git diff --staged                比较暂存区和版本库差异

git diff --cached                比较暂存区和版本库差异

git diff HEAD                显示工作区与当前分支最新commit之间的差异

git diff [branch1] [branch2]                在两个分支之间比较

git diff [commit1][commit2]                比较两次提交之间的差异

git show [commit]                显示某次提交的元数据和内容变化

git show --name-only [commit]                显示某次提交发生变化的文件

git show [commit]:[filename]                显示某次提交时,某个文件的内容


撤销

git reset --hard [commit]                重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致

git reset --hard HEAD^                一个^表示后退一步,n个^表示后退n步(使用^符号只能后退)

git reset --hard HEAD~n                表示后退n步(使用^符号只能后退)

git revert [commit]                新建一个commit,用来撤销指定commit

git revert HEAD                撤销前一次 commit


分支

git branch                列出所有本地分支

git branch -v                列出所有本地分支最后提交信息

git branch -r                列出所有远程分支

git branch -r -v                列出所有远程分支最后提交信息

git branch -a                列出所有本地分支和远程分支

git branch [branch-name]                新建一个分支,但依然停留在当前分支

git checkout -b [branch]                新建一个分支,并切换到该分支

git branch [branch] [commit]                新建一个分支,指向指定commit

git branch --track [branch] [remote-branch]                新建一个分支,与指定的远程分支建立追踪关系

git checkout [branch-name]                切换到指定分支,并更新工作区

git checkout -                切换到上一个分支

git branch --set-upstream-to [branch] [remote-branch]                建立追踪关系,在现有分支与指定的远程分支之间

git merge [branch]                合并指定分支到当前分支

git cherry-pick [commit]                选择一个commit,合并进当前分支

git branch -d [branch-name]                删除分支

git br -D [branch-name]                强制删除某个分支

git push origin --delete [branch-name]                删除远程分支

Git push origin [冒号][你的分支名字]                删除远程分支

git branch -dr [remote/branch]                删除远程分支(例如:git branch -dr origin/test)


远程同步

git clone [url]                克隆远程仓库

git remote -v                查看远程服务器地址和仓库名称

git remote show [remote]                显示某个远程仓库的信

git remote add [别名] [远程地址]                增加一个新的远程仓库,并命名

git remote set-url origin [URL]                更换远程仓库地址

git remote rm [别名]                删除远程仓库

git remote rename [oldname] [newname]                修改某个远程仓库在本地的简称

git fetch [remote] [branch]                下载远程仓库指定分支的所有变动

git merge [remote/branch]                 将远程仓库分支合并到本地当前分支

git pull [remote] [branch]                取回远程仓库的变化,并与本地分支合并(pull=fetch+merge)

git push [remote] [branch]                推送本地分支到远程仓库

git push [remote] --force                强行推送当前分支到远程仓库,即使有冲突

git push [remote] --all                推送所有分支到远程仓库


Git 别名

如果不想每次都输入完整的 Git 命令,可以通过 git config 文件来轻松地为每一个命令设置一个别名

git config alias.co checkout (仅在当前本地库范围内有效)

git config --global alias.co checkout (全局有效)

设置了上述别名后:git checkout [branch]  等价于  git co [branch]


参考:

http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

https://www.cnblogs.com/karry990921/p/9014791.html




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值