IntelliJ IDEA With Git | 8lovelife's life
0%

IntelliJ IDEA With Git

记录下Git如何与IntelliJ IDEA协作

环境准备

IntelliJ IDEA With Git 开发过程

1. 初次获取远端代码

  • 使用IntelliJ IDEA Terminal

image

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

2. 查看远端仓库分支

1
2
3
4
5
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. 将指定的远端分支同步到本地(建议同远端名一致)

1
2
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. 查看本地当前所在分支 & 关联的远端分支

1
2
3
4
5
6
7
8
9
10
11
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. 准备在指定分支开发

1
2
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

1
2
3
4
5
6
7
8
9
10
11
12
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. 撤销本地的修改

image

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

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
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. 切换到主分支 & 合并修改到主分支

1
2
3
4
5
6
7
8
9
10
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.本地分支同步到远端分支(会改变远程分支的文件)

1
2
3
4
5
6
7
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

1
2
3
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上

1
2
3
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 开发过程

本地分支重命名

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

本地新建分支推送到远端

1
2
3
4
5
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'.

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

1
2
3
4
5
6
7
8
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. 查看分支历史变动
1
2
3
4
5
6
7
8
9
10
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

I have always been so scared, so alone. - Chihiro Ogino

Spirited Away