DEV Community

Cover image for [Git] 5 Git Tips I Learned Today
Kitco's Today I Learned
Kitco's Today I Learned

Posted on

[Git] 5 Git Tips I Learned Today

How to Stop Tracking Unnecessary Files in Git

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/
Enter fullscreen mode Exit fullscreen mode

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

After running the command, you should see output like this:

delete '.idea/fileName.xml'
...
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To verify the configuration:

git config --global --list | grep merge.tool
Enter fullscreen mode Exit fullscreen mode

When a conflict occurs, run:

git mergetool
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Use this command to see differences in IntelliJ:

git difftool
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Then, set the upstream branch:

git branch --set-upstream-to=origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

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

git fetch
Enter fullscreen mode Exit fullscreen mode

You can verify the remote branches with:

git branch -r
Enter fullscreen mode Exit fullscreen mode

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →