CodeGym /행동 /Kotlin SELF /Pro-level tools and problem solving

Pro-level tools and problem solving

Kotlin SELF
레벨 18, 레슨 4
사용 가능

So far, we have been looking at an ideal scenario: you write code, make commits, create a Pull Request. But in real life, something often goes wrong: you forgot to add a file, made a typo in the commit message, or realized you were heading in the wrong direction. In this lecture, we will go through how to solve the most common problems.

1. Rollback — discard local changes before a commit

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

Use the Rollback feature:

  1. Open the Commit tab on the left panel.
  2. Find the modified file in the list (or select multiple files) that you want to “roll back”.
  3. Right-click it and select 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 undo local experiments.

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

Scenario: you just clicked Commit 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 Amend (add/fix):

  1. In the Commit window, make the necessary 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 the message from your previous commit. 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 via Push yet, and then realized you rushed. You want the commit to disappear from history, but you want the actual code changes to remain in your editor so you can continue working.

Use Undo Commit:

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

Select a 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 have lost nothing and can calmly continue writing code.

4. Reset — deleting local history (Be careful!)

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

Scenario A: Undo the last local commits

You made several commits locally (before Push) and realized the whole development direction 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 mistaken commits).
  3. Right-click it and select Reset Current Branch to Here....

Scenario B: Reset to the server state

You experimented locally, got tangled in conflicts, and the project no longer builds. 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, this 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 select Reset Current Branch to Here....
Reset Current Branch window

Reset modes

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

  • Mixed / Soft: Safe mode. Commits will be removed from history, but all file changes 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. Permanently deletes both commits and all uncommitted code changes. Your project will fully roll back to the selected commit state. This is exactly what you need for Scenario B so 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.

As soon as a commit reaches the remote server, it becomes part of the shared project history. Trying 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 push it with a normal push. This is absolutely normal practice. If the mistake is related to functionality or code logic, it is better to write the fix manually. A linear history where a faulty commit 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 faulty commit in Git -> Log and choose Revert Commit, the IDE will automatically create a new “anti-commit” that does exactly the opposite of what the faulty one did (for example, it will remove the added lines).

When should you use Revert?

Although the button looks magical, you should use it carefully for complex code. When commit history turns into a chain like “Commit -> Commit -> Revert -> Commit”, your teammates may literally “break their heads” during code review trying to understand what ultimately works and what has been undone.

However, Revert Commit works perfectly for isolated text edits. For example, marketing asked you to update the text on the home page. You committed 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 a perfectly 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 the 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 enhanced analogue — Shelve.

  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, commit, and push it. When the urgent task is done, return 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. Look into the past: advanced work with the log

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

  • Commit hash: you have probably noticed a line 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 specific part of the functionality and when.
  • Want to find the commit where a specific line of code was added or removed? The Log window has 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 still 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 disaster. It can irreversibly delete commits that your teammates have already pulled and are working from.

When you absolutely MUST NOT use force push:

  • On any shared branches: main, develop, master. Never.
  • On any branch that someone other than you is working on.

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 dropdown menu of the Push button. IDE developers did this intentionally so you do not click it by accident.

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 calling it out as a separate rule.

Always click Update Project before starting work on a new task, and every morning at the start of your workday. This will save you from many future merge conflicts and ensure you start with the most up-to-date code, having pulled 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, grouping them by tasks.

Group 1: Viewing and analysis (Safe actions)

  • Copy Revision Number — copies the unique commit hash to the clipboard. Useful when 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 understand: “What did this code look like a month ago?”
  • Show Repository at Revision — opens a window where you can see the project’s file structure exactly as it was at the time of that commit (without changing your current working branch).

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 that commit. You will end up in a “detached HEAD” state. 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 moving

  • Cherry-Pick (The cherry on top) — an incredibly useful command! It lets you “pluck” a single 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 been pushed to the server yet (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 that commit (removing the history ahead of it).
  • Revert Commit — creates an “anti-commit” for safe undoing on the server.
  • Undo Commit... — breaks a local commit back into the changes window.

Group 5: Rewriting history

The menu also includes commands like Squash Commits... (combine several 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, because they completely rewrite history. In real team work, they are used to “polish” your draft local branch before sending it for review (creating a Pull Request).

11. Parting advice

Version control systems may seem complex: branches, merges, conflicts, scary commit hashes. But remember the main thing: Git is your safety net. Every senior developer has at some point panic-googled how to get out of “detached HEAD” or how to undo an accidental push. Do not be afraid to experiment. Making mistakes locally is absolutely normal — you now have a 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 run 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. 90% of developers run into this problem 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, selecting your new account.
  4. Now open 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 (main) account, even though you definitely pushed under the new profile’s identity!

Investigation:

Your goal is to figure out why this happened. Hint: authenticating on the GitHub website (to have Push permissions) 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 the settings of your main account. Make a new commit and ensure the history shows a commit from account number 2.

1
설문조사/퀴즈
Gitgwa GitHub sogae, 레벨 18, 레슨 4
사용 불가능
Gitgwa GitHub sogae
Beojeon gwalli: Git mit GitHub hwalyong
코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION