- 撤销上一次所有add
git reset --soft|mixed|hard--softDoes not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it. 切换到另一次commit,该commit之后的修改都被放在暂存区--mixedResets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.切换到另一次commit,该commit之后的修改都被放在工作目录--hardResets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. 切换到另一次commit,该commit之后的修改都被舍弃
- 删除分支
//删除本地分支git branch -d dev_20181018//删除远程分支git push origin --delete dev
- 取消跟踪已跟踪的文件
git rm --cached readme.txt
- git 添加远程仓库
git remote add <远程仓库名> <URL>
- git 删除远程仓库
git remote remove origin//重命名git remote rename origin test
- git clone 特定仓库
git clone -b <分支名> <远程仓库地址>
- 显示远程分支
git ls-remote <remote>git remote show <remote>
- 推送分支
//来将本地的 serverfix 分支推送到远程仓库上的 awesomebranch 分支。git push origin serverfix:awesomebranchgit push <remote> <branch>:
- 打标签
ag的位置是固定的,在给指定提交打好标签以后,它就固定于此位置。 分支的位置会不断变动的,随着分支的向前推移或者向后回滚,都在不断变化。
//-m 选项指定了一条将会存储在标签中的信息。//如果没有为附注标签指定一条信息,//Git 会启动编辑器要求你输入信息。git tag -a v1.4 -m "my version 1.4"后期打标签你也可以对过去的提交打标签。git tag -a v1.2 9fceb02//标签推送至远端//将本地v1.0的tag推送到远端服务器git push origin v1.0//push所有tag,命令格式为:git push [origin] --tags
- git比较分支提交的不同
//commit 后面的箭头,根据我们在 –left-right dev…master 的顺序,//左箭头 < 表示是 dev 的,右箭头 > 表示是 master的git log --left-right dev...master
git cherry-pick有冲突解决办法
一般情况下,直接使用cherry-pick产生的冲突只是因为文件重复编辑造成的,可以直接手动解决冲突即可,但是有时候A分支的patch_01依赖了其它的patch,但是你仅仅想cherry_pick patch_01的更改到分支B,如果直接使用cherry-pick, git 会默认个其依赖的patch也cherry-pick到分支B.可以使用如下方法解决:
## cherry-pick到其他分支假设打算同步dev-brd-20q3分支的代码至prod-nome2-skywell-2020-06分支,约定好的同步topic为/sync_patch1. 定位到patches所在的模块2. 输入指令:git format-patch remotes/origin/prod-nome2-skywell-2020-06...remotes/origin/dev-brd-20q33. 此时会产生分离后所有的patches(不是不同的patches),需要找到上次同步的点和之后需要同步的patches,如:0687-Bug-36832-Fix-bug-of-update-section-to-next.patch4. 切换到需要同步的分支prod-nome2-mz-2020-06git checkout -b prod-nome2-skywell-2020-06 origin/prod-nome2-skywell-2020-065. 输入指令:git am 0687-Bug-36832-Fix-bug-of-update-section-to-next.patcha. 如果发生冲突git statusgit apply --reject 0687-Bug-36832-Fix-bug-of-update-section-to-next.patchb .找到*.rej,解决这些文件里提示的冲突;git add rejected_filesgit am --resolvedc. 如果已经cherry-pickgit am --skip6. 同步到分支,输入指令:git push origin HEAD:refs/for/prod-nome2-skywell-2020-06/V2X_V2V-prod-nome2-skywell-2020-06# 注意:1. 若从同步点到需要的pahch的中间还有其他的patches,如果直接插入需要的patch,中间可能会出错,此时,需要把中间依赖的patches依次插入进去。
git 更改某个提交内容或
将当前改动追加到某次commit 上
尤其适用于gerrit 上的CL 未submit 之前,有多个patch时的多个功能点并行开发。rebase 前,可以创建一个分支,用于rebase:git checkout -b rebase_tmp直接更改某次提交(改动某个指定的commit)将HEAD移到需要更改的commit上:git rebase -i 0bdf89^找到需要更改的commit, 将行首的pick改成edit, 按esc, 输入:wq退出(弹出的交互式界面中显示的commit 信息,与git log 显示的顺序相反,即父节点在上显示)更改文件使用git add 改动的文件添加改动文件到暂存使用git commit --amend追加改动到第一步中指定的commit上使用git rebase --continue移动HEAD到最新的commit处解决冲突:编辑冲突文件, 解决冲突git add .git commit --amend解决冲突之后再执行git rebase --continue
将工作空间中的改动追加到某次提交上**note:**第0步和第2步(加粗)与上面步骤不同,其余步骤相同保存工作空间中的改动git stash将HEAD移到需要更改的commit上:git rebase f744c32^ --interactive找到需要更改的commit, 将行首的pick改成edit, 按esc, 输入:wq退出执行命令git stash pop使用git add 改动的文件添加改动文件到暂存使用git commit --amend追加改动到第一步中指定的commit上使用git rebase --continue移动HEAD到最新的commit处解决冲突:编辑冲突文件, 解决冲突git add .git commit --amend解决冲突之后再执行git rebase --continue
