安装完Git后进行设置用户名和邮箱:
git config --global user.name "xxx"
git config --global user.email "xxx"
Example:
git config --global user.name "test"
git config --global user.email "[email protected]"
初始化仓库,将当前目录变成Git仓库,生成一个 .git 文件来管理此仓库:
git init
添加文件到暂存区(添加未提交):
git add <file>
Example:
git add test.txt
添加所有修改文件到暂存区 :
git add .
删除文件 :
git rm -- <file>
Example:
git rm -- test.txt
提交带有说明的文件到版本库的分支:
git commit -m "xxx"
Example:
git commit -m "这是一次测试提交"
查看仓库文件当前的状态:
git status
查看文件修改了什么内容:
git diff
查看本地工作区和仓库最新版本区别(–后有一个空格):
git diff HEAD -- test.txt
查看从最近到最远所提交的日志(版本回退后,不包括回退后版本日志):
git log
查看从最近到最远所提交的日志(版本回退后,不包括回退后版本日志),每条提交日志一行显示 :
git log --pretty=oneline
查看所有提交的日志 :
git reflog
版本回退到上个版本(–hard HEAD^^上2个版本/–hard HEAD~100 上一百个版本) :
git reset --hard HEAD^
回退到指定版本 :
git reset --hard <版本提交ID>
Example:
git reset --hard 6d5123
重新将暂存区文件放回工作区(未添加状态) :
git reset HEAD <file>
Example:
git reset HEAD test.txt
查看文件内容 :
cat <file>
Example:
cat test.txt
撤销工作区(未添加到暂存区)文件的修改(- - 必须要,不然就是切换分支) :
git checkout -- <file>
Example:
git checkout -- test.txt
切换到名为xxx的分支
git checkout <xxx>
Example:
git checkout master
创建名为xxx的分支 :
git branch <xxx>
Example:
git branch test
创建名为xxx的分支,并切换到该分支 :
git checkout -b <xxx>
Example:
git checkout -b test
查看所有分支,当前分支带 * 号 :
git branch
查看当前所有分支(包含远程分支)以及各自分支最新提交信息 :
git branch -av
删除名为xxx的分支 :
git branch -d <xxx>
Example:
git branch -d test
创建SSH KEY (ssh-keygen不能有空格) :
ssh-keygen -t rsa -C "邮箱"
Example:
ssh-keygen -t rsa -C "[email protected]"
添加本地仓库内容到远程仓库 :
git remote add <ssh地址>
Example:
git remote add git@github.YourName/xxxxxxx.git
推送所有内容到远程仓库(第一次需要加 -u) :
git push -u origin master
将名为xxx的本地分支推送到远程仓库为远程分支 :
git push origin <xxx>
Example:
git push origin test
拉取远程库内容 :
git pull
查看远程库信息 :
git remote
查看远程库详细信息 :
git remote -v
合并名为 xxx 分支到当前分支——不能看出合并历史 :
git merge <xxx>
Example:
git merge test
禁用Fast forward,合并名为 xxx 分支到当前分支——能看出合并历史 :
git merge --no-ff -m "提交描述" <xxx>
Example:
git merge --no-ff -m "这是一个禁用FF的合并" <test>
储藏当前工作状态(工作区变成clean状态) :
git stash
查看储藏的工作状态 :
git stash list
恢复储藏工作状态,但是该状态的内容依然存在 :
git stash apply
删除使用git stash apply恢复储藏状态的内容
git stash drop
恢复储藏工作状态,并删除内容 :
git stash pop
多次stash,恢复指定stash :
git stash apply stash@{0}
给当前分支添加xxx标签,方便查找 :
git tag <xxx>
Example:
git tag v1.0
给commit id的提交打上xxx标签 :
git tag <xxx> <commit id>
Example:
git tag <v1.0> <6da123>
查看标签 :
git tag
根据标签名查看标签详细信息 :
git show <xxx>
Example:
git show <v1.0>
删除名为 xxx 的标签 :
git tag -d <xxx>
Example:
git tag -d v1.0
推送名为 xxx 的标签至远程库
git push origin <xxx>
Example:
git push origin v1.0
推送所有标签至远程库 :
git push --tags
克隆远程库代码 :
git clone <xxx>
Example :
git clone [email protected]/xxxxxxx.git
删除远程库标签分2步:
1.本地删除名为xxx的标签
git tag -d <xxx>
Example:
git tag -d v1.0
2.删除远程库xxx标签
git push origin :refs/tags/<xxx>
Example:
git push origin :refs/tags/v1.0