CodeGym /Courses /Kotlin SELF /Professional tools and problem solving

Professional tools and problem solving

Kotlin SELF
Level 18, Lesson 4
Available

So far we have considered an ideal scenario: you write code, make commits, and create a Pull Request. But in real life, things often go wrong: you forgot to add a file, made a typo in the commit message, or realized you went in the wrong direction. In this lecture, we will go over how to solve the most common problems.

1. Rollback — reverting local changes before a commit

Scenario: you changed a file, but realized all your edits are wrong and want to quickly return the file to the state it was saved in in the last commit.

Use the Rollback function:

  1. Open the Commit tab on the left panel.
  2. Find the changed file in the list (or select multiple files) that you want to “roll back”.
  3. Right-click it and choose Rollback (or click the rounded-arrow icon).

The IDE will warn you that unsaved changes will be lost. Confirm, and the file will instantly return to its last “clean” version from Git. This is the simplest and safest way to cancel local experiments.

2. The “Amend” magic: how to add to the last commit

Scenario: you just clicked the Commit button and immediately realized you forgot to include an important file (for example, an updated test) or made a silly typo in the commit message. Making a new commit with a message like “Added the forgotten file” is bad form.

Instead, use the Amend function (add/fix):

  1. In the Commit window, make the required fixes in the forgotten file and check it.
  2. In the Commit Message panel, find the Amend checkbox (or the gear icon -> Amend Commit) and enable it.
  3. The IDE will pull in your previous commit message. You can edit it.
  4. Click Commit.

Instead of creating a new “junk” commit, Git will neatly “open up” your previous commit, add the new files, update the message, and seal it back up. The history will remain perfectly clean!

Read more about this feature in the official JetBrains tutorial.

3. Undo Commit

Scenario: you made a commit but have not pushed it yet via Push, and then realized you rushed. You want the commit to disappear from history, but you want the code changes to stay in your editor so you can keep working.

Use the Undo Commit function:

  1. Open the history: the Git -> Log tab at the bottom.
  2. Right-click the topmost (your latest) commit.
  3. Select Undo Commit.

Select the changelist (usually Default Changelist) and click OK. The commit will disappear from history, and all your files will return to the Commit window with the modified status. You did not lose anything and can calmly continue writing code.

4. Reset — deleting local history (Caution!)

The Reset Current Branch to Here... function is a powerful tool that moves your branch pointer to any selected commit. Depending on the situation, this button can both gently undo recent actions and radically wipe out broken code.

Scenario A: Undoing the latest local commits

You made several commits locally (before Push) and realized that the whole line of development was a mistake.

  1. Open the Git -> Log tab.
  2. Find the last “good” commit you want to return to (the one before the series of incorrect ones).
  3. Right-click it and choose Reset Current Branch to Here....

Scenario B: Reset to the server state

You experimented locally, got tangled in conflicts, and the project stopped building. You want to say: “Git, delete everything I messed up here, and make my branch exactly as clean as it currently is on the GitHub server.”

  1. Open Git -> Log.
  2. Find the remote branch marker in the list. Usually it is a commit with a purple label origin/main (or origin/imya_vashey_vetki). This is the latest known state of the code on the server.
  3. Right-click that commit and choose Reset Current Branch to Here....
Reset Current Branch window

Reset modes

In both scenarios, the IDE will prompt you to choose a reset mode. For beginners, it is important to know two of them:

  • Mixed / Soft: Safe mode. The commits will be removed from history, but all changes in files will remain in your editor (like a big Undo). Perfect for Scenario A if you want to rebuild the commits from scratch.
  • Hard: Radical mode. Irreversibly deletes both commits and all uncommitted changes in the code. Your project will fully roll back to the state of the selected commit. This is the mode needed for Scenario B so that your local branch becomes an exact copy of the server. Use it only if you are 100% sure you will never need the locally written code again.

5. What to do after Push?

Scenario: you did a push, and only then noticed a mistake in it.

Once a commit gets to the remote server, it becomes part of the shared project history. An attempt to rewrite that history (for example, via Reset or Amend) can create huge problems for your teammates. Imagine they have already pulled your changes and started building their work on top of them.

The correct and safe solution:

Just make a new commit that fixes the mistake and send it with a regular push. This is absolutely normal practice. If the issue is related to functionality or code logic, it is better to write the fix by hand. A linear history where buggy code is followed by a clear fix commit makes the project’s evolution transparent for the whole team.

The IDE also has a special Revert Commit function. If you right-click the incorrect commit in the Git -> Log and choose Revert Commit, the IDE will automatically create a new “anti-commit” that does exactly the opposite of what the incorrect one did (for example, removing the added lines).

When should you use Revert?

Although the button looks magical, using it for complex code must be done with caution. When commit history turns into a chain like “Commit -> Commit -> Revert -> Commit”, your teammates doing code review can literally “break their heads” trying to understand what ultimately works and what has been reverted.

However, Revert Commit works perfectly for isolated text edits. For example, marketing asked you to update the text on the home page. You made a commit and pushed, and an hour later the business changed its mind and asked to restore the old text. In this case, one-click Revert will bring everything back as it was, save you time, and leave an absolutely clear trace in history.

6. Stash / Shelve: how to temporarily hide changes

Scenario: you are actively coding a new feature; all files are red and blue. Suddenly your team lead runs in: “Drop everything — we urgently need to fix a bug in the main branch!” You cannot switch to main because you have unfinished conflicting changes. You also cannot make a commit with half-written code.

The solution is to temporarily “hide” your work in a virtual box. In Git this is called Stash, and IntelliJ IDEA has its own improved counterpart — Shelve (put on the shelf).

  1. Open the Commit window and select the modified files.
  2. Right-click and choose Shelve Changes... (or Git -> Stash Changes).
  3. Enter a clear name for this “bookmark” and click Shelve.

Your files will instantly return to their original state. Now you can safely switch to main, fix the bug, make a commit, and push it. When the urgent task is done, go back to your working branch, open the Shelf tab (Shelf), right-click your hidden changes, and choose Unshelve. Your unfinished code will return to its place, and you can continue working!

7. Looking into the past: advanced work with the log

We are already familiar with the Git -> Log window, but it is not just a list of commits — it is a powerful investigation tool.

  • Commit hash: you have probably noticed a string of letters and digits in the log, for example a1b2c3d. This hash is a unique identifier for each “snapshot” of your code. Commit hash in logs
  • You can filter history by branch, author, date, or even by a word in the commit message. This helps you quickly find who worked on a particular part of the functionality, and when.
  • Want to find the commit where a specific line of code was added or removed? In the Log window there is a search field that searches not only messages, but also the contents of the changes themselves.
  • Right-click in the code editor gutter (where the line numbers are) and select Annotate with Git Blame. The IDE will show next to each line who last changed it and in which commit. This is incredibly useful for understanding why the code is written the way it is.

8. Danger zone: Force Push

So, we have established that changing pushed commits is a bad idea. But what if you still changed your local history (for example, using Undo Commit or Amend), while the server already has the old version? When you try to do a normal push, Git will throw an error, protecting the shared history from being overwritten.

For such cases, there is an option — force push. This command tells the server: “Forget everything you had. My local version of history is the only correct one. Replace your history with mine.”

WARNING! In 99% of cases, using force push in team projects is a catastrophe. It can irreversibly delete commits that your teammates have already pulled and are building their work upon.

When you must NEVER use force push:

  • On any shared branches: main, develop, master. Never.
  • On any branch that anyone else is working on besides you.

The only acceptable scenario:

You are working in your personal feature branch that nobody has seen or pulled yet. You made several “dirty” commits, pushed them to the server, and then decided to squash them or fix a typo via Amend. In this case, you can do a force push to update your branch on the server before creating a Pull Request.

In the IDE, this option is usually hidden in the drop-down menu of the Push button. The IDE developers did this intentionally so you do not click it accidentally.

Force Push button

If you are not 100% sure what you are doing, never use force push. It is safer to create a new commit with fixes (or use Revert).

9. Updating the project

We already talked about this in the second lecture, but this topic is so important for teamwork that we are making it a separate rule.

Always click Update Project before starting work on a new task and every morning at the beginning of your workday. This will save you from many future merge conflicts and guarantees that you start with the most up-to-date version of the code, pulling all the latest commits from your teammates from the server.

10. Git Log context menu

When you open the Git -> Log tab and right-click any commit, a huge context menu appears. Let’s go over the most useful commands by grouping them by tasks.

Group 1: Viewing and analysis (Safe actions)

  • Copy Revision Number — copies the unique commit hash to the clipboard. Useful if you need to send a link to a commit to a teammate in chat or reference it in a task (Jira/YouTrack).
  • Compare with Local — lets you compare the state of any file from that old commit with what you currently have unsaved in the editor (in the working directory). A great way to answer: “What did this code look like a month ago?”
  • Show Repository at Revision — opens a window where you can see the structure of all project files exactly as they were at the time of that commit (while your current working branch does not change).

Group 2: Working with branches

  • New Branch... — creates a new branch directly from the selected commit in the past. Indispensable if you have moved far ahead, realized you broke everything, and want to start a new branch from the last “working” point.
  • Checkout Revision — switches your local project to this commit. You will end up in a state of “detached HEAD”. This means you can run the old code and explore it, but if you start making new commits, they will not be attached to any branch. To return to normal work, simply check out your usual branch (for example, main).

Group 3: Changing and porting

  • Cherry-Pick (The cherry on top) — an incredibly useful command! It allows you to “pull out” one specific commit from someone else’s (or your other) branch and copy it into your current branch. For example, someone fixed a bug in another branch that blocks your work — you just cherry-pick that commit into your branch without merging the entire other branch.
  • Edit Commit Message... — if the commit has not yet been pushed to the server (no Push), you can quickly rename its message. Much more convenient than Amend if you only need to change the text.

Group 4: Undoing changes

We covered these commands in detail in the previous sections:

  • Reset Current Branch to Here... — moves your current branch to this commit (deleting the history ahead).
  • Revert Commit — creates an “anti-commit” for safe undo on the server.
  • Undo Commit... — breaks a local commit back into the changes window.

Group 5: Rewriting history

The menu also includes commands such as Squash Commits... (combine multiple commits into one), Drop Commits (remove a commit from the middle of history), or Interactively Rebase from Here.... These are tools for powerful surgical work on local history (Interactive Rebase).

Tip:

While you are learning, try not to use these commands, as they completely rewrite history. In real team work, they are used to “polish” your rough local draft branch before sending it for review (creating a Pull Request).

11. Final note

Version control systems can seem complicated: branches, merges, conflicts, scary commit hashes. But remember the main thing: Git is your safety net. Every senior developer once panic-googled how to exit “detached HEAD” or how to undo an accidental push. Do not be afraid to experiment. Making mistakes locally is absolutely normal, because now you have the full arsenal of IntelliJ IDEA tools to undo, roll back, and fix any slip-up before the team sees it. Commit often, write clear messages, always do Update Project in the mornings, and you will become a reliable and professional contributor to any project.

12. Bonus practical task

We suggest you complete a small but very tricky task. This issue is encountered by 90% of developers when they start working on multiple projects.

Your task:

  1. Register a second GitHub account with a different email address.
  2. Add this new account to IntelliJ IDEA.
  3. Create a new repository, clone it, write a couple lines of code, and do Commit and Push, choosing your new account.
  4. Now go to the git-log page and carefully look at the commit history.

What is the catch?

Most likely, you will be surprised to find that the commit author is listed as your first (primary) account, even though you definitely pushed as the new profile!

Investigation:

Your goal is to figure out why this happened. Hint: authorization on the GitHub website (to have Push rights) and the author signature inside the commit itself are not the same thing. Find a way (via the terminal?) to switch user.name and user.email so that they apply only to this specific project, without breaking your primary account settings. Make a new commit and achieve the result that the history contains a commit from account number 2.

1
Survey/quiz
Introduction to Git and GitHub, level 18, lesson 4
Unavailable
Introduction to Git and GitHub
Version control: working with Git and GitHub
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION