git with gerrit 常用操作解析

https://blog.csdn.net/weixin_44722978/article/details/94438640

问题:在mster分支写了半天,然后git commit 提交了 ,才发现 在masrter分支开发的。

解决:

git reset HEAD~
HEAD 代表:上一次提交

这样 刚刚提交的就又回到本地的local changes 列表中。
nice
继续 切换分支,重新提交

时刻查看当前branch是个好习惯。

git reset --hard 放弃本地修改

https://blog.csdn.net/zyfzhangyafei/article/details/78805357

如果想放弃本地的文件修改,可以使用git reset --hard FETCH_HEAD,FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull.
注意:

git merge会形成MERGE-HEAD(FETCH-HEAD) 。git push会形成HEAD这样的引用。HEAD代表本地最近成功push后形成的引用。

就 我的经验,有时候会莫名其妙地出现这种状况,而且Untracked files 还特别多(实际上自己可能只改了一两个文件),所以只好先保存好自己确定做出的local的修改,然后用git reset --hard FETCH_HEAD回到上次成功pull之后的点,然后再pull就没有问题了

https://blog.csdn.net/smithallenyu/article/details/50175503

git是目前比较流行且很好用的分布式代码管理工具。相信很多人已经在使用它了。

这篇文件主要介绍些git的常见操作, 对于日常应用是足够了的。

后续会介绍一些git的不常用但却很有用的操作。

先看张图, 了解下git各个操作阶段的含义, 通俗易懂的。

下面介绍常用的基本操作。

1. clone

1)clone某个分支

git clone xxxx(repository url) -b yyyy(branch name)


2)浅clone, truncated to the specified number of revisions

git clone xxxx --depth 1(最小是1, 必须是正整数)

在git1.9版本以上, 可以直接在浅clone的基础上,push 到remote


3)large repository的clone

在clone大的项目时,有时会遇到“fatal: The remote end hung up unexpectedly | fatal: early EOF | fatal: index-pack failed”的错误,硬件的过载保护  

step1: 浅clone

git clone xxxx --depth 1

step2: 恢复完全repository

git fetch --unshallow


2. commit

1) git commit -am “xxxx” # -a(--all)即commit所有修改的且已经tracked过(存在于Index暂存

   区)的文件。


2) 撤销已经入本地库的commit, 并回到前一个commit

git reset --hard HEAD~1


3) 撤销已经入本地库的commit, 且保留该commit的修改

git reset --soft HEAD~1


4) 撤销本地的修改

git reset --hard HEAD


5) 撤销对某个文件的修改并回到上一个commit的状态

git checkout -- filename OR git checkout HEAD filename


6) 合并若干个commit为一个

git rebase -i HEAD~X # X is the count of commits to merge, X>=2

7) 撤销某个历史commit的内容

git revert commitID, 可能需要解冲突。

revert只是撤销那次commit的提交内容, 但是那次历史的commit仍然会保留在提交的历史记录中(可用git log看到)。


3. 分支

1)查看远程分支及本地分支

git branch -a


2)查看远程分支

git branch -r


3)checkout 到一个已有分支

git checkout xxxx(xxxx is the branch name exists in remote repository)


4)新建一个分支(分支不存在)

git checkout -b xxxx(xxxx is the branch name to create)


5)新建一个分支(无论存在与否)

git checkout -B xxxx


6)删除一个本地分支

git branch -d xxxx # 删除一个已经merged的branch

git branch -D xxxx # 删除一个branch不管是否merged

7)删除一个远程分支

git push origin :xxxx OR git push origin --delete xxxx

8) 合并一个本地分支

e.g. 当前分支是master, git merge xxxx


4. stash 隐藏暂存

将修改的文件,且经过add暂存的,隐藏起来。

1) 最简单的使用:git stash,然后就变干净了(到HEAD),git stash pop

2) 复杂点的:

git stash save “message1”

git stash save “message2”

git stash apply "stash@{0}" | git stash apply "stash@{1}"


5. log

1) 查看graph tree

git log --graph


2) log 一行显示

git log --pretty=oneline


3) 限制显示的commit个数

git log -n N


4) 时间范围

git log --after="2015-1-1" --before="2015-1-4"


5) 自定义格式

git log --pretty=format:"%cn committed %h on %cd"


6. pull

git pull origin branch_name == git fetch origin branch_name && git merge FETCH_HEAD


1)通常的使用场景

本地commit之后,在push之前,先git pull, 若有冲突, 需要fix再commit(此commit是merge commit)。实际上,git pull = git fetch origin master&& git merge FETCH_HEAD OR git fetch origin && git merge origin/master


7.push

1) 直接提交入库

git push origin branch_name

2) 提交到CR

git push origin HEAD:refs/for/branch_name

不建议直接在命令行添加reviewer, 到web页面添加较好。


8. merge 与rebase的区别使用

本地已经commit, 且remote repository已经发生改变

方法1:pull || fetch + merge

若有冲突, 需要解决,并commit(merge commit)


方法2:fetch + rebase

若有冲突, 需要解决, 并commit(原来的commit放在远程的HEAD之后,且无新的commit产生, 但commitId改变了)。 e.g. git fetch origin && git rebase origin/master &&(resolve conflict) && git add changed_files && gir rebase --continue


8. CR


1) 打patch

git commit --amend , 在message中, 添加单独的一行Change-Id: Ixxxx(Ixxxx是change-id, 可从gitweb页面得到)


2) 提交的CR,出现cannot merge的情况的处理方法

出现的原因是,merge conflict, 即remote的head已经发生了改变

解决方法:

在本地, git fetch origin

git rebase origin/master # 解决冲突若需要

git commit --amend, 将Change-Id 加入message

git push origin HEAD:refs/for/master
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值