-
git log:查看修改得历史记录
-
git log --graph --pretty=oneline --abbrev-commit
-
–graph 图形
-
–pretty=oneline 减少数据
-
–abbrev-commit 头部数据减少
-
-
git reflog:记录 commit 命令
step 1:上传文件到远程
-
git add:
-
file_name:将指定文件保存到暂存区
-
.:将所有新增、修改得文件保存到暂存区
-
-
git commit -m "add or modify file"
-
git push origin master:refs/for/master
-
git push <远程主机名> <本地分支名>:<远程分支名>
-
refs/for 的意义在于我们提交代码到服务器之后是需要经过code review 之后才能进行merge的
-
git push origin master:refs/for/master:将本地的master分支推送到origin主机,同时指定origin为默认主机
-
step 2:撤销修改
-
未使用 git add 缓存代码时:
-
git checkout -- file_name/. :撤销工作区的修改
-
命令 git checkout -- file_name 中的--很重要,没有--,就变成了“切换到另一个分支”的命令
-
-
已经使用了 git add 缓存了代码:
-
git reset HEAD file_name/.
-
-
已经用 git commit 提交了代码:
-
git reset --hard HEAD^ :来回退到上一次commit的状态;
-
git reset --hard commit_id:回退到任意版本
-
-
已经用 git push推到远程仓库:
-
git reset --soft commit_id
-
注意 --hard 参数会抛弃当前工作区的修改
-
使用 --soft 参数的话会回退到之前的版本,但是保留当前工作区的修改,可以重新提交
-
step 3:分支管理
-
git checkout:
-
git checkout <分支名>:切换分支
-
git checkout -b <分支名>:新建并切换分支
-
-
git switch :
-
git switch <分支名>:切换分支
-
git switch -c <分支名>:新建并切换分支
-
-
git merge dev
-
换回
master 后,
把dev
分支的工作成果合并到master
分支
-
-
git merge --no-ff -m "merge with no-ff" dev
-
本次合并要创建一个新的 commit,所以加上
-m
参数,把commit描述写进去
-
-
git rebase
-
把本地未push的分叉提交历史整理成直线
-
step 4:拉最新主干
-
git pull <远程主机名> <远程分支名>:<本地分支名>
-
如果远程分支是与当前分支合并,则冒号后面的部分可以省略:
-
-
git pull origin master:
-
git fetch + git merge
-
-
git pull --rebsae origin master :
-
git fetch + git rebase
-
step 5:git fetch
-
git fetch <远程主机名> <分支名> //注意之间有空格
-
取回更新后,会返回一个
FETCH_HEAD
,指的是某个branch在服务器上的最新状态,我们可以在本地通过它查看刚取回的更新信息
-
-
git pull <远程主机名> <远程分支名>:<本地分支名>:
-
git fetch origin master //从远程主机的master分支拉取最新内容
-
git merge FETCH_HEAD //将拉取下来的最新内容合并到当前所在的分支中
-
-
git fetch 拉取 patch 代码,并切换到该分支
-
git fetch <代码库> <patch>
-
git checkout FETCH_HEAD
-
step 6:git diff
-
git diff commit_id_1 commit_id_2 > diff.file
-
获取两个 commit id 的 diff 文件
-
-
patch -p1 < diff.file:
-
将 diff 文件写入代码
-
step 7:git blame
-
git blame 查询代码的修改信息,不加参数 -L 查询整个文件修改信息
-
e.g. git blame -L 281,281 <文件相对路径>
说明:
-
在合入主干之前,只能提交一次 commit,如果多次push到远程且未合入主干,将commit到本地仓库的代码回滚到未add之前,已做的过的更改不会被撤销;
-
多次commit,而没有push到远程,使用 commit --amend 即可;
-
git commit --amend -m "information" : 代码合入后重新提交代码