git clone
$ git clone 仓库地址
$ git clone -b 分支名 仓库地址
git config
# 查看配置信息
# --local:仓库级,--global:全局级,--system:系统级
$ git config <--local | --global | --system> -l
# 查看当前生效的配置信息
$ git config -l
# 编辑配置文件
# --local:仓库级,--global:全局级,--system:系统级
$ git config <--local | --global | --system> -e
# 添加配置项
# --local:仓库级,--global:全局级,--system:系统级
$ git config <--local | --global | --system> --add <name> <value>
# 获取配置项
$ git config <--local | --global | --system> --get <name>
# 删除配置项
$ git config <--local | --global | --system> --unset <name>
# 配置提交记录中的用户信息
$ git config --global user.name <用户名>
$ git config --global user.email <邮箱地址>
# 更改Git缓存区的大小
# 如果提交的内容较大,默认缓存较小,提交会失败
# 缓存大小单位:B,例如:524288000(500MB)
$ git config --global http.postBuffer <缓存大小>
# 调用 git status/git diff 命令时以高亮或彩色方式显示改动状态
$ git config --global color.ui true
# 设置别名
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.last 'log -1 HEAD' // 最后一次提交
# 配置可以缓存密码,默认缓存时间15分钟
$ git config --global credential.helper cache
# 配置密码的缓存时间
# 缓存时间单位:秒
$ git config --global credential.helper 'cache --timeout=<缓存时间>'
# 配置长期存储密码
$ git config --global credential.helper store
- 文件的状态变化周期:
git status
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean // 所有已跟踪文件在上次提交后都未被更改过
$ echo 'My Project' > README
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
nothing added to commit but untracked files present (use "git add" to track) //未跟踪文件,需要git add跟踪
$ git add README
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: README // 已暂存状态
$ git status -s // 状态简览
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt // 新添加到暂存区中的文件前面有 A 标记,修改过的文件前面有 M 标记
git diff
$ git diff // 比较的是工作目录中当前文件和暂存区域快照之间的差异(修改之后还没有暂存起来的变化内容)
$ git diff --staged/cached // 已暂存文件与最后一次提交的文件差异
git stash
可用来暂存当前正在进行的工作,比如想 pull 最新代码又不想 commit,或者另为了修改一个紧急的 bug ,先 stash,使返回到自己上一个 commit,,改完 bug 之后再 stash pop , 继续原来的工作;
添加缓存栈: git stash ;
查看缓存栈: git stash list ;
推出缓存栈: git stash pop ;
取出特定缓存内容: git stash apply stash@{1} ;
git commit
$ git commit -m ''
$ git commit -am 'added new benchmarks'
git rm 删除
$ git rm -f project.md // 从暂存区域移除
git mv 移动
$ git mv file_from file_to
=
$ mv README.md README
$ git rm README.md
$ git add README
git log 查看提交历史
$ git log -p -2 // -p 或 --patch ,会显示每次提交所引入的差异(按 补丁 的格式输出)。 你也可以限制显示的日志条目数量,例如使用 -2 选项来只显示最近的两次提交
$ git log --stat // 每次提交的简略统计信息
$ git log --pretty=oneline // 将每个提交放在一行显示,另外还有 short,full 和 fuller 选项,它们展示信息的格式基本一致,但是详尽程度不一
$ git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
撤销操作
- git commit --amend 漏掉了几个文件没有添加,或者提交信息写错了,使用amend 选项的提交命令来重新提交。(最终你只会有一个提交——第二次提交将代替第一次提交的结果。)
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
- git reset HEAD fileName 取消暂存的文件(还原提交记录)
- git revert 生成一个新的提交来撤销某次提交,此次提交之前的所有提交都会被保留。
- git checkout -- fileName 撤消对文件的修改
在 Git 中任何 已提交 的东西几乎总是可以恢复的,然而,任何你未提交的东西丢失后很可能再也找不到了。
远程仓库
- git remote 查看你已经配置的远程仓库服务器
- git remote -v 显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL
- git remote rename pb paul 远程仓库的重命名
- $ git remote remove paul 远程仓库的移除
git tag
$ git tag
$ git tag -l //列出所有tag
$ git tag -a v1.2 9fceb02 // 后期打标签(sha)
$ git tag -d v1.4-lw // 删除标签
git branch
$ git branch
$ git branch newBranchName
$ git branch -a // 查看远程分支名
$ git checkout -b branchName
$ git branch -d branchName
$ git branch -m [<原分支名称>] <新的分支名称> // 修改分支名
git reflog
git reflog的功能是查看本地操作记录,可以看到本地的commit, merge, rebase等操作记录,并带有版本号
git merge
# 把指定的分支合并到当前所在的分支下
$ git merge <分支名称>
git pull
# 从远程仓库获取最新版本。
$ git pull
=
$ git fetch + git merge
$ git pull --rebase //todo
$ git merge --no-ff //todo
git cherry-pick
把已经提交的记录合并到当前分支。
# 把已经提交的记录合并到当前分支
$ git cherry-pick <commit ID>
git add 提交到暂存区,出错怎么办
一般代码提交流程为:工作区 -> git status 查看状态 -> git add . 将所有修改加入暂存区-> git commit -m "提交描述" 将代码提交到 本地仓库 -> git push 将本地仓库代码更新到 远程仓库
- 场景1:工作区
当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
// 丢弃工作区的修改 git checkout -- <文件名> - 场景2:暂存区
当你不但改乱了工作区某个文件的内容,还 git add 添加到了暂存区时,想丢弃修改,分两步,第一步用命令
就回到了场景1,第二步按场景1操作。git reset HEAD <file>// 把暂存区的修改撤销掉(unstage),重新放回工作区。 git reset HEAD <文件名>
git commit 提交到本地仓库,出错怎么办?
-
- 提交信息出错 更改 commit 信息
git commit --amend -m“新提交消息” -
- 漏提交 commit 时,遗漏提交部分更新,有两种解决方案:
方案一:再次 commit
git commit -m“提交消息”此时,git 上会出现两次 commit
方案二:遗漏文件提交到之前 commit 上
git add missed-file // missed-file 为遗漏提交文件 git commit --amend --no-edit--no-edit 表示提交消息不会更改,在 git 上仅为一次提交
提交错误文件,回退到上一个 commit 版本,再 commit
-
git reset 删除指定的 commit
// 修改版本库,保留暂存区,保留工作区 // 将版本库软回退1个版本,软回退表示将本地版本库的头指针全部重置到指定版本,且将这次提交之后的所有变更都移动到暂存区。 git reset --soft HEAD~1 // 修改版本库,修改暂存区,修改工作区 //将版本库回退1个版本,不仅仅是将本地版本库的头指针全部重置到指定版本,也会重置暂存区,并且会将工作区代码也回退到这个版本 git reset --hard HEAD~1 // git版本回退,回退到特定的commit_id版本,可以通过git log查看提交历史,以便确定要回退到哪个版本(commit 之后的即为ID); git reset --hard commit_id -
git revert
撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销 作为一次最新的提交
// 撤销前一次 commit git revert HEAD // 撤销前前一次 commit git revert HEAD^ // (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。 git revert commitgit revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去, 版本会递增,不影响之前提交的内容
git merge 与 git rebase 的区别是?
- rebase会合并该分支与其他分支的commit history,可能会得到一个新的commit history
- rebase得到更简洁的项目历史,去掉了merge commi,如果合并出现代码问题不容易定位,因为re-write了commit history
- merge会创建新的commit,包括每个分支的commit 详情
- 每次merge会自动产生一个merge commit,特别是commit比较频繁时,看到分支很杂乱。
- 想要得到一个干净的,没有merge commit的线性commit历史记录,选择git rebase
- 想要得到一个完整的commit历史记录,且想避重写commit历史记录的风险,选择git merge