[Git] 5 Git Tips I Learned Today

[Git] 5 Git Tips I Learned Today

How to Stop Tracking Unnecessary Files in Git, Setting IntelliJ as the Default Merge Conflict Tool & More

·

3 min read

How to Stop Tracking Unnecessary Files in Git

Studied on 2nd March 2025,

While working on a team project, I noticed that the .idea/ folder kept being tracked by Git. This folder contains IDE-generated metadata and configuration files, which don't need to be pushed to the remote repository.

Even if .idea/ is listed in .gitignore, Git might still track it if the folder was already added before the .gitignore rule was applied.

To stop tracking the folder without deleting the actual files, run:

git rm --cached -r .idea/

This removes the folder from Git's index without affecting the local files.

Useful Things I Learned

  • Hidden folders like .idea/ won't show up with ls. Use ls -a to see all hidden files and folders.

  • The .gitignore file needs to be in the root directory of the project, not inside subfolders.

  • The /out folder, which stores compiled binaries or build artifacts, should also be excluded from the repository since these files can be regenerated from the source code.

Discard Local Changes and Sync with Another Branch

If you want to discard all local changes and sync your branch with the latest version of another branch, use:

git reset --hard origin/<branch_name>

This command completely resets your branch to match the remote branch, deleting any local changes.

Setting IntelliJ as the Default Merge Conflict Tool

If you're not comfortable resolving merge conflicts from the command line, IntelliJ can be used to visually resolve conflicts.

On Mac:

git config --global merge.tool intellij
git config --global mergetool.intellij.cmd "/Applications/IntelliJ IDEA.app/Contents/MacOS/idea diff $LOCAL $REMOTE $BASE $MERGED"
git config --global mergetool.prompt true

On Windows:

git config --global merge.tool intellij
git config --global mergetool.intellij.cmd "C:/Program Files/JetBrains/IntelliJ IDEA/bin/idea64.exe" diff $LOCAL $REMOTE $BASE $MERGED
git config --global mergetool.prompt true

To verify the configuration:

git config --global --list | grep merge.tool

When a conflict occurs, run:

git mergetool

IntelliJ will display the conflicting files. The left pane shows the local changes (HEAD), and the right pane shows the incoming changes from the remote branch. After resolving conflicts, click Accept and commit the merge result:

git add .
git commit -m "Resolved merge conflicts"

Viewing Git Diff in IntelliJ

To set IntelliJ as the default diff tool:

git config --global diff.tool intellij
git config --global difftool.intellij.cmd "/Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea diff $LOCAL $REMOTE"

Use this command to see differences in IntelliJ:

git difftool

IntelliJ will open each file one by one, showing the changes side-by-side.

Connecting an Existing Local Branch to a Remote Branch

If you create a local branch without pushing it to the remote repository, you'll need to connect it manually.

First, push the branch to the remote repository:

git push origin <branch_name>

Then, set the upstream branch:

git branch --set-upstream-to=origin/<branch_name>

If the remote branch doesn't show up immediately, run:

git fetch

You can verify the remote branches with:

git branch -r

This ensures your local branch is properly tracking the remote branch.