JavaRush /Java Blog /Random EN /Making Git friends with Intellij IDEA

Making Git friends with Intellij IDEA

Published in the Random EN group
By tradition, I welcome you, future Senior Software Engineers. Making Git friends with Intellij IDEA - 1Today we'll talk about the logical continuation of my article about Git . I also recommend reading the material on branching strategieswhich I posted earlier. In the article about Git, I described how to work with it on the command line, and today I will show you how to do all this in Intellij IDEA. At the beginning of my journey as a developer, I used the command line and thought that there was no need for me to use the UI for this business. After all, everything is clear and so ... But it was exactly until the moment when I started using Git in Intellij IDEA ... I want to say right away that I will describe my personal work experience. There are several options for solving the same tasks through Intellij IDEA, and if you know how to do better what I will describe in the article, write in the comments and discuss.

Required input:

  1. Read, repeat and understand the git article . This will help ensure that everything is already set up and ready to go.
  2. Install Intellij IDEA.
  3. Allocate an hour of personal time for complete assimilation.
For work, let's take the demo project that I used for the article about git. UPDATE:at the time of publication, the new github UI will already be available, and some icons will not be where it is shown in the article. Don't be afraid: you just need to either not switch to the new UI, or look for them.

Cloning the project locally

There are two options here.
  1. If you already have a github account and want to push something later, it’s better to fork the project to yourself and clone your copy. How to fork - I described in this article in the chapter an example of the forking workflow .
  2. Clone from my repository and do everything locally without the ability to push the whole thing to the server. After all, this will be my repository))
To clone a project from github, you need to copy the link to the project and transfer it to IntelliJ IDEA:
  1. Copy the project address:

    Making Git friends with Intellij IDEA - 2
  2. Open Intellij IDEA and select Get from Version Control:

    Making Git friends with Intellij IDEA - 3
  3. Copy paste the address on the project:

    Making Git friends with Intellij IDEA - 4
  4. You will be prompted to create an Intellij IDEA project. We accept an offer:

    Making Git friends with Intellij IDEA - 5
  5. Since there is no build system, and this is not the purpose of the article, we select Create project from existing sources :

    Making Git friends with Intellij IDEA - 6
  6. Next, there will be such an oil painting: Making Git friends with Intellij IDEA - 7We figured out cloning, now you can look around.

First look at Intellij IDEA as a git UI

Take a closer look at the cloned project again: already there you can get a lot of information about the version control system. The first is the Version Control panel in the bottom left corner. In it, you can find all local changes and get a list of commits (similar to git log). Let's move on to the Log lecture . There is a certain visual component that helps to understand exactly how the development process went. For example, you can see that a new branch was created with the added header to txt commit , which was then merged into the master branch. If you click on a commit, in the right corner you can see all the information about the commit: all changes and its metadata. Making Git friends with Intellij IDEA - 8Moreover, you can see what changes have been made. Moreover, the conflict was resolved there. This IDEA also shows great. If you double-click on a file that was changed during this commit, we will see how the conflict was resolved: Making Git friends with Intellij IDEA - 9It is noticeable that on the right and on the left there were two versions of one file that needed to be merged into one. And in the middle - the result, which in the end turned out. When a project has a lot of branches, commits and users who work in the project, you need to search separately by branch (branch), user (user) and date (date): And the last thing I want to Making Git friends with Intellij IDEA - 10explain before starting work is how understand which branch we are in. I give you a minute to search ... did you find it? Give up? :D There is a Git button in the bottom right corner : master, where after Git: shows which branch the project is currently on. If you click on the button, you can do a lot of useful things: move to another branch, create a new one, rename an existing one, and so on. Making Git friends with Intellij IDEA - 11

Working with the repository

Useful hotkeys

To continue working, you need to remember a few very useful hotkeys:
  1. ctrl + t - get the latest changes from the remote repository (git pull).
  2. ctrl + k - commit / see all the changes that are currently. This includes both untracked and modified files (see my git article for details) (git commit).
  3. ctrl + shift + k is the command to push changes to a remote repository. All commits that were created locally and are not yet on the remote will be offered for push (git push).
  4. alt + ctrl + z - roll back changes in a specific file to the state of the last created commit in the local repository. If you select the entire project in the upper left corner, you can roll back changes to all files.
Making Git friends with Intellij IDEA - 12

What do we want?

For us to work, we need to master the basic script that is used everywhere. The task is to implement new functionality in a separate branch and push it to a remote repository (then you need to create another pull request for the main branch, but this is beyond the scope of our article). What do I need to do?
  1. Get all changes so far on the master branch (master for example).

  2. On the basis of this main one, create a separate one for your work.

  3. Implement new functionality.

  4. Switch to the master branch and check if there were any new changes while working. If not, then everything is fine, but if it was, then we do the following: go to the working branch and rebase the changes from the main branch to ours. If everything went well, then great. But there may well be conflicts. And they can just be solved in advance, without wasting time on a remote repository.

    It would seem, why do it? This is a good practice that prevents conflicts from occurring after pushing your branch to the local repository (there is, of course, a chance that there will still be conflicts, but it becomes much less).

  5. Push your changes to the remote repository.
Further already depends on your tasks and imagination.

Get changes from remote server?

I added the description to the README with a new commit and I want to get these changes. A choice is offered between merge and rebase in case changes were made both in the local repository and on the remote one. Choose a merge. Enter ctrl + t : Making Git friends with Intellij IDEA - 13As a result, you can see how the README has changed, i.e. changes from the remote repository have been pulled up, and in the lower right corner you can see all the details of those changes that came from the server. Making Git friends with Intellij IDEA - 14

Create a new branch based on master

Everything is simple here.
  1. Go to the lower right corner and click on Git: master , select + New Branch .

    Making Git friends with Intellij IDEA - 15
  2. We leave a checkmark Checkout branch and write the name of the new branch. For me it would be readme-improver .

    Making Git friends with Intellij IDEA - 16

    After that, Git: master will change to Git: readme-improver .

Simulating parallel work

For conflicts to appear, someone has to create them :D I'll edit the README with a new commit through the browser and thus simulate parallel work. They say someone during my work made changes in the same file as me, which will lead to a conflict. I will remove the word “completely” from line 10.

Implement your functionality

The task is to change the README and add a description to the new article, that is, that the work in the git goes through Intellij IDEA. Adding this: Making Git friends with Intellij IDEA - 17Changes made, now you can create a commit. We press the hot key ctrl + k , we get: Making Git friends with Intellij IDEA - 18Before creating a commit, you need to carefully look at what is offered in this window. I specifically added arrows where to look. There are many interesting things there. In the Commit Message section , write the text of the commit, and in order for it to be created, you need to click the Commit button. I never found how to make it a hotkey, so if someone finds it, write, I will be very happy. We write that the README has changed and create a commit. As a result, an alert pops up in the lower left corner, in which the name of the commit will be: Making Git friends with Intellij IDEA - 19

Check if master branch has changed

We completed the task, it works, the tests were written, everything is fine. But before pushing to the server, you still need to check if there have been any changes in the main branch during this time. How could this happen? Very simple: someone was given a task after you, and that someone did it faster than you. Therefore, we switch to the master branch. To do this, in the lower right corner, do what is shown in the figure below: Making Git friends with Intellij IDEA - 20In the master branch, press ctrl + t to get its latest changes from the remote server. If you look at what the changes were, you can easily see what happened: Making Git friends with Intellij IDEA - 21As you can see, the word “completely” was removed. Perhaps it was someone from marketing who decided that this was not the way to write and gave the developers the task of updating it. We now have the latest version of the master branch locally. Let's go back toreadme-improver . Now we need to rebase the changes from the master branch to ours. Do: Making Git friends with Intellij IDEA - 22If you did everything right with me, the result should be a conflict in the README file: Making Git friends with Intellij IDEA - 23There is also a lot of information to understand and absorb. This shows a list (one element in our case) of files that have conflicts. We can choose three options:
  1. accept yours - accept only changes from readme-improver.
  2. accept theirs - accept only changes from master.
  3. merge - choose what to leave and what to remove.
It is not clear what has changed there, and if the changes are already in the master, then they are needed there, and it is impossible to simply accept our changes, so we select merge : Making Git friends with Intellij IDEA - 24Here you can see that there are three parts:
  1. These are the changes from the readme-improver.
  2. Result. So far, it's the way it was before the changes.
  3. Changes from the master branch.
We need to collect the result in such a way that it satisfies everyone. Therefore, we studied what they did BEFORE us, realized that they simply removed the word “completely”. Well, okay, no problem. So we will remove it as a result and add our changes. As soon as we fix the result, you can click Apply . After that, a notification will pop up that the rebase was successful: Making Git friends with Intellij IDEA - 25This is how we resolved our first conflict through Intellij IDEA :D

Push changes to remote server

The next step is to push the changes to the remote server and create a pull request. To do this, simply press ctrl + shift + k , after which we get: Making Git friends with Intellij IDEA - 26On the left there will be a list of commits that are not pushed to the remote repository, and on the right there will be all the files that have changed. And that's all: press Push , and you will be happy :) If the push is successful, there will be such a notification in the lower right corner: Making Git friends with Intellij IDEA - 27

Bonus part

I did not want to initially add the creation of a pull request to the article, but it turns out not very complete because of this. Therefore, we go to the github repository (if it is yours, of course))) ) and see that the github already knows what to offer us: Making Git friends with Intellij IDEA - 28Click on Compare & pull request , then click Create pull request . Due to the fact that we resolved conflicts in advance, now when creating a pull request, you can immediately merge it: Making Git friends with Intellij IDEA - 29That's all I wanted to tell you this time. Of course, I only opened the door for you and showed you a small part. You will find the rest as needed. Traditionally, I invite you to subscribe to my GitHub account, where I post development projects on various technologies that I use at work. I recently had a personal achievement - my project has already been evaluated by more than a hundred developers. It's an incredible feeling of joy that what you've done is being used by someone. And use it to your advantage. Making Git friends with Intellij IDEA - 30

useful links

  1. CodeGym: Getting Started with Git: A Complete Beginner's Guide
  2. GitHub: Demo project for work
  3. CodeGym: Exploring Gita Branching Strategies
  4. JetBrains: Set up a Git Repository
  5. Habr: Git rebase
  6. GitHub: My account
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION