Intellij IDEA 使用 GIT


3/12/2018 IDEA

在 IDEA 中操作 Git

GitLab 中有一个 git-branch-test 的项目

14908545096082

处于 master 分支

本地项目目录: 14908548696226

所在分支: 14908548840403

一般的工作流程:

  1. master 作为主分支,一般都是用来发布最终版本的分支
  2. 当实现一个新需求时,需要创建一个分支,在新创建的分支上进行开发

这是已经实现的功能,并且已经发布到gitlab服务器上的分支 (master):

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int a = Integer.parseInt(s);
        System.out.println(a);
        if (isOdd(a))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }
    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     */
    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

现在有一个新功能需要开发: 增加一个交换2个参数的值的方法

因为是新的需求,我们不能直接在 master 分支上修改,必须新建一个分支,功能完成后经过测试才能正式提交到主分支上

新建分支

IDEA 设置 一次就好

  1. 设置 tasks 14908583751226

token 登录自己的 gitlab 进行申请 User Settings --> Access Tokens

设置成功后, IDEA 右上角会出现 task 下拉选择框

14908588867839

  1. 新建分支

14908625120015

"ok" 之后 自动切换到 sprint1分支

开始新需求的开发

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int input = Integer.parseInt(s);
        System.out.println(input);
        if (isOdd(input))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }

        Integer[] value = {100,1};
        switchValue(value);
        System.out.println("value[0] = " + value[0] + " \n" + "value[1] = " + value[1]);

    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     */
    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }

    /**
     * Switch value.
     * 交换值
     * @param value the value
     */
    public static void switchValue(Integer[] value){
        // todo 未完成
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

假设此时, 你突然接到一个电话说有个很严重的问题需要紧急修补, 那么可以按照下面的方式处理:

  1. 提交当前未完成的工作到本地工作区
  2. 返回到 master 分支.
  3. 为这次紧急修补建立一个新分支, 并在其中修复问题.
  4. 通过测试后, 回到 master 分支, 将修补分支合并进来, 然后再推送到 gitlab 服务器上.
  5. 切换到之前实现新需求的分支, 继续工作.

这里使用 IDEA 自带工具进行分支的创建以及切换

使用 IDEA 创建分支

先提交当前未完成的工作到本地工作区

14908588073283

然后切换到 master

创建分支 14908589410833

然后会自动切换到 iss55 分支上 14908590481527

所有代码都保持为 master 原样

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int a = Integer.parseInt(s);
        System.out.println(a);
        if (isOdd(a))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }
    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     */
    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

现在开始修复 bug

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int a = Integer.parseInt(s);
        System.out.println(a);
        if (isOdd(a))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }
    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     * 2017-03-30 15:35 dong4j
     * 修复输入负数时都为 false 的 bug
     */
    public static boolean isOdd(int i) {
        return (i & 1) == 1;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

测试一番后提交到本地工作区

14908595942883

然后回到 master 分支, 把它合并进来,然后发布到 gitlab

合并分支 IDEA 中操作

14908597786589

合并之后:

14908598080074

合并之后 master 分支和 iss55 分支指向同一位置 14908599153259

然后将修改 push 到 gitlab

14908601651972

push 成功之后, origin 指向最新的一条记录

此时 iss55问题已被修复,可以删除掉

IDEA 上操作

14908600354489

最后回到原来的 sprint1分支上继续完成没有完成的工作 14908627662830

代码变成:

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int input = Integer.parseInt(s);
        System.out.println(input);
        if (isOdd(input))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }

        Integer[] value = {100,1};
        switchValue(value);
        System.out.println("value[0] = " + value[0] + " \n" + "value[1] = " + value[1]);

    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     */
    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }

    /**
     * Switch value.
     * 交换值
     * @param value the value
     */
    public static void switchValue(Integer[] value){
        // todo 未完成
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

此时可以看到 iss55 分支修改的代码在 sprint1分支中并没有改变

不用担心之前 iss55 分支的修改内容尚未包含到 sprint1 中来. 如果确实需要纳入此次修补, 可以用git merge master 把 master 分支合并到iss55; 或者等 sprint1 完成之后, 再将 sprint1 分支中的更新并入 master.

先完成 sprint1 的开发工作

public class HelloWorld {
    public static void main(String[] args) {
        String s = JOptionPane.showInputDialog("请输入一个整数");
        int input = Integer.parseInt(s);
        System.out.println(input);
        if (isOdd(input))
           System.out.println("奇数");
        else {
            System.out.println("偶数");
        }

        Integer[] value = {100,1};
        switchValue(value);
        System.out.println("value[0] = " + value[0] + " \n" + "value[1] = " + value[1]);

    }

    /**
     * Is odd boolean.
     * 检测奇偶性
     * @param i the
     * @return the boolean
     */
    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }

    /**
     * Switch value.
     * 交换值
     * @param value the value
     */
    public static void switchValue(Integer[] value){
            value[0] = value[0] ^ value[1];
            value[1] = value[0] ^ value[1];
            value[0] = value[0] ^ value[1];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

完成开发,提交到本地工作空间

14908607033975

合并分支

在问题 sprint1 相关的工作完成之后, 可以合并回 master 分支. 实际操作同前面合并 iss55 分支差不多, 只需回到master 分支, 运行git merge 命令指定要合并进来的分支:

IDEA 操作与上面合并操作一样

14908609351798

最后 push 到 gitlab

Last Updated: 7/3/2019, 6:17:56 PM