IntelliJ IDEA With Git

记录下Git如何与IntelliJ IDEA协作

环境准备

IntelliJ IDEA With Git 开发过程

1. 初次获取远端代码

  • 使用IntelliJ IDEA Terminal

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2uotZYcM-1675266252937)( https://8lovelife-1256398294.cos.ap-shanghai.myqcloud.com/Intellij%20Idea/IdeTerminal.png)]

Mac:code mac$ git clone https://github.com/grpc/grpc-java.git
Cloning into 'grpc-java'...

2. 查看远端仓库分支

Mac:dmz-inward- mac$ git branch -av
* master                 0388b70 some feture
  remotes/origin/HEAD    -> origin/master
  remotes/origin/feature cd52891 some features
  remotes/origin/master  0388b70 some feture

3. 将指定的远端分支同步到本地(建议同远端名一致)

Mac:dmz-inward- mac$ git checkout -b feature origin/feature
Branch 'feature' set up to track remote branch 'feature' from 'origin'.
Switched to a new branch 'feature'

4. 查看本地当前所在分支 & 关联的远端分支

Mac:dmz-inward- mac$ git branch  // 查看当前分支
* feature
  master
  
Mac:dmz-inward- mac$ git branch -avv  // 查看当前分支关联的远端分支
* feature                bb87089 [origin/feature] Merge branch 'feature_test' into feature
  feature_test           dd90129 //
  master                 0388b70 [origin/master] some feture
  remotes/origin/HEAD    -> origin/master
  remotes/origin/feature bb87089 Merge branch 'feature_test' into feature
  remotes/origin/master  0388b70 some feture
  • View 确认

image

5. 准备在指定分支开发

Mac:dmz-inward- mac$ git checkout -b feature_test
Switched to a new branch 'feature_test'

6. coding & show diff

  • coding
  • 随时查看本地的变动,防止遗漏:

image

image

7. 将文件添加到暂存区 & 提交工作分支的修改 | 更改提交的comments

Mac:dmz-inward- mac$ git add . // 添加暂存区

Mac:dmz-inward- mac$ git commit -m "feature add test" // 提交
[feature_test 7590940] feature add test
 2 files changed, 7 insertions(+)
 create mode 100644 dmz-inward-test/src/test/java/Test.java
 
Mac:dmz-inward- mac$ git commit --amend  // 修改提交的comments
[feature_test 90b9e43] feature add retests
 Date: Mon May 21 10:40:06 2018 +0800
 2 files changed, 7 insertions(+)
 create mode 100644 dmz-inward-test/src/test/java/Test.java
  • 暂存区与HEAD

image

8. 撤销本地的修改

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KZkboOe4-1675266252938)( https://8lovelife-1256398294.cos.ap-shanghai.myqcloud.com/Intellij%20Idea/revert.png)]

9. 切换到与远端同步的主分支 & 同步可能的远端修改

Mac:dmz-inward- mac$ git checkout feature  // 切换分支
Switched to branch 'feature'
Your branch is up to date with 'origin/feature'.

Mac:dmz-inward- mac$ git pull  // 同步远端可能的修改
Already up to date.

10. 切换到工作分支 & rebase | resoleve conflicts

Mac:dmz-inward- mac$ git checkout feature_test 
Switched to branch 'feature_test'

Mac:dmz-inward- mac$ git rebase feature
Current branch feature_test is up to date.

若rebase遇到冲突则手动解决,利用Intelli Idea 的show diff 可以清晰观察冲突点
冲突解决后进行:
git add * 
git rebase --continue

11. 切换到主分支 & 合并修改到主分支

Mac:dmz-inward- mac$ git checkout feature
Switched to branch 'feature'
Your branch is up to date with 'origin/feature'.

Mac:dmz-inward- mac$ git merge --no-ff feature_test
Merge made by the 'recursive' strategy.
 dmz-inward-test/src/test/java/Test.java     | 6 ++++++
 dmz-inward-test/src/test/java/TestFast.java | 1 +
 2 files changed, 7 insertions(+)
 create mode 100644 dmz-inward-test/src/test/java/Test.java
  • 分支

image

12.本地分支同步到远端分支(会改变远程分支的文件)

Mac:dmz-inward- mac$ git push
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (17/17), 1.36 KiB | 698.00 KiB/s, done.
Total 17 (delta 10), reused 0 (delta 0)
remote: Resolving deltas: 100% (10/10), completed with 4 local objects.

Git 相关

Git 常用的操作命令

回退到分支指定版本

  • 查看分支历史变动历史,回退指定版本号

image

Mac:dmz-inward- mac$ git reset --hard bb8708981a01eb568d114ed7ddad71f6cd881f7e
HEAD is now at bb87089 Merge branch 'feature_test' into feature


Cherry-Pick

将某一分支的commit修改,应用到当前本地分支。如:将feature上的某一commit修改应用到master上

Mac:dmz-inward- mac$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
  1. Cherry-Pick

image

  1. Intellij Idea With Git 开发过程

本地分支重命名

Mac:dmz-inward- mac$ git branch -m feature feature_rename

本地新建分支推送到远端

Mac:dmz-inward- mac$ git push --set-upstream origin feature_one
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/dreamming/dmz-inward-.git
 * [new branch]      feature_one -> feature_one
Branch 'feature_one' set up to track remote branch 'feature_one' from 'origin'.

删除本地分支 & 删除远端分支

Mac:dmz-inward- mac$ git branch -d feature_one  // 删除本地分支
warning: deleting branch 'feature_one' that has been merged to
         'refs/remotes/origin/feature_one', but not yet merged to HEAD.
Deleted branch feature_one (was dd90129).

Mac:dmz-inward- mac$ git push origin :feature_one  // 删除远端分支
To https://github.com/dreamming/dmz-inward-.git
 - [deleted]         feature_one

查看版本变动

  1. 查看分支历史变动
Mac:dmz-inward- mac$ git reflog
47128b4 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: commit: //
0388b70 HEAD@{1}: checkout: moving from feature to master
bb87089 (feature_two, feature) HEAD@{2}: checkout: moving from master to feature
0388b70 HEAD@{3}: checkout: moving from feature to master
bb87089 (feature_two, feature) HEAD@{4}: reset: moving to bb8708981a01eb568d114ed7ddad71f6cd881f7e
06d0579 (origin/feature) HEAD@{5}: commit: __
bb87089 (feature_two, feature) HEAD@{6}: checkout: moving from master to feature
0388b70 HEAD@{7}: checkout: moving from feature_one to master


image

  1. 查看文件更改人

image

image

参考

我的博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值