编辑提交
显示刚才提交的内容
git show
或者
git log -n1 -p
我的提交信息写错了
如果你的提交信息写错了,且这次提交还没有push,你可以通过下面方法来修改提交信息
git commit --amend --only
这会打开默认编辑器GNU nano
- 直接修改commit 信息
- ctrl+x,如果有修改信息,则会选择Y/N,
- 然后显示保存文件名字,可以直接按enter
一条命令一次完成
git commit --amend --only -m 'xxxxx'
修改完后,可以git push -f强制推送
我提交(commit)里的用户名和邮箱不对
如果是单次commit 修改
git commit --amend --author "New Authorname <authormail@mydomain.com>"
我想从一个提交里移除一个文件
# 会生成一个上个commit的file的状态在暂存区
git checkout HEAD^ filename
# 需要重新commit之后上个file状态才真正修改成在之前commit的内容
git add .
git commit --amend
这方式会把那个不要文件的修改内容丢弃
查看历史记录
git log --pretty=oneline # 查看提交记录
git reflog # 查看包括reset的一些信息,只在本地,远程拉取没有
打补丁diff 和 patch
两者区别:
- .diff文件只是记录文件改变的内容,不带有commit记录信息,多个commit可以合并成一个diff文件。
- .patch文件带有记录文件改变的内容,也带有commit记录信息,每个commit对应一个patch文件。
生成版本文件
git diff commit_id1 commit_id2 > a.diff
git format-patch commit_id -n
应用patch 和 diff
检查patch/diff是否能正常打入:
git apply --check 【path/to/xxx.patch】
git apply --check 【path/to/xxx.diff】
打入patch/diff:
git apply 【path/to/xxx.patch】
git apply 【path/to/xxx.diff】
或者
git am 【path/to/xxx.patch】
Stash
暂存所有改动
git stash
暂存指定文件
git stash push filename1 filename2
暂存时记录消息
git stash save <message>
git stash push -m <message>
使用某个指定暂存
首先你可以查看你的stash记录
git stash list
然后你可以apply某个stash
git stash apply "stash@{n}"
此处,'n'是stash在栈中的位置,最上层的stash会是0,除此之外,也可以使用时间标记
git stash apply "stash@{2.hours.ago}"
我想删除一个分支
删除一个远程分支:
git push origin --delete my-branch
或者
git push origin :my-branch
删除一个本地分支:
git branch -D my-branch
拉取clone之后才新推上去的分支
首先,从远程拉取所有分支:
git fetch --all
git checkout branch-name