环境配置
git config user.name your_name
: 设置你的用户名, 提交会显示git config user.email your_email
: 设置你的邮箱git config core.quotepath false
: 解决中文文件名显示为数字问题
操作
基本操作
-
git init
: 初始化一个 git 仓库 -
git add <filename>
: 添加一个文件到 git 仓库中git add
. : stages new and modified, without deletedgit add -u : stages modified and deleted, without new
git add -u : stages All
- git rm
- rm <filename> : 本地删除,暂存区未删除
- git rm <filename> : 本地删除, 暂存区删除
- git rm --cached <filename> : 本地未删除,暂存区删除
-
git checkout -- <file>
: 还原未暂存(staged)的文件 -
git reset
-
git reset HEAD <filename>
: 取消暂存,那么还原一个暂存文件,应该是先reset
后checkout
-
git reset --hard <commit>: 替换引用的指向,替换暂存区,替换工作区
-
git reset --soft <commit>: 只更改引用的指向,不改变暂存区和工作区
-
-
git stash
: 隐藏本地提交记录, 恢复的时候git stash pop
。这样可以在本地和远程有冲突的情况下,更新其他文件 -
git status
: 查看你的代码在缓存与当前工作目录的状态
-
git log
: 显示分支中提交更改的记录
-
git commit -m "commit message"
: 提交到本地 -
git push [remote-name] [branch-name]
: 把本地的提交记录推送到远端分支 -
git pull
: 更新仓库 git pull
= git fetch
+ git merge
git commit -m "commit message"
: 提交到本地git push [remote-name] [branch-name]
: 把本地的提交记录推送到远端分支git pull
: 更新仓库 git pull
= git fetch
+ git merge
分支
git branch <branch-name>
: 基于当前 commit 新建一个分支,但是不切换到新分支git checkout -b <branch-name>
: 新建并切换分支git checkout <branch-name>
: 切换分支git branch -d <branch-name>
: 删除分支git push origin <branch-name>
: 推送本地分支git checkout -b <local-branch-name> origin/<origin-branch-name>
: 基于某个远程分支新建一个分支开发git checkout --track origin/<origin-branch-name>
: 跟踪远程分支(创建跟踪远程分支,Git 在git push
的时候不需要指定origin
和branch-name
,其实当我们clone
一个 repo 到本地的时候,master
分支就是 origin/master 的跟踪分支,所以提交的时候直接git push
)。git push origin :<origin-branch-name>
: 删除远程分支
比较不同
- 查看变更还未载入(changed but unstaged)的文件比对: git diff
- 查看载入(stage)而并未提交(commit)的变更: git diff --stage
- 查看在最后一次提交之后的所有变更: git diff HEAD
- 从一个特定点开始文件的修改情况: git diff v1.6.0 -- README
- 两次提交的差异比对: git diff v1.0 v1.1
- git diff v1.6.1.1 v1.6.1.2 --stat 两个版本之间差异的统计
- git diff v1.6.1.1 v1.6.1.2 -- sha1_file.c 某个具体文件的变更比对
-
git commit -m "commit message"
: 提交到本地 -
git push [remote-name] [branch-name]
: 把本地的提交记录推送到远端分支 -
git pull
: 更新仓库git pull
=git fetch
+git merge