[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 …


This content originally appeared on DEV Community and was authored by Kitco's Today I Learned

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/

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'
...

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.


This content originally appeared on DEV Community and was authored by Kitco's Today I Learned


Print Share Comment Cite Upload Translate Updates
APA

Kitco's Today I Learned | Sciencx (2025-03-02T13:36:17+00:00) [Git] 5 Git Tips I Learned Today. Retrieved from https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/

MLA
" » [Git] 5 Git Tips I Learned Today." Kitco's Today I Learned | Sciencx - Sunday March 2, 2025, https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/
HARVARD
Kitco's Today I Learned | Sciencx Sunday March 2, 2025 » [Git] 5 Git Tips I Learned Today., viewed ,<https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/>
VANCOUVER
Kitco's Today I Learned | Sciencx - » [Git] 5 Git Tips I Learned Today. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/
CHICAGO
" » [Git] 5 Git Tips I Learned Today." Kitco's Today I Learned | Sciencx - Accessed . https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/
IEEE
" » [Git] 5 Git Tips I Learned Today." Kitco's Today I Learned | Sciencx [Online]. Available: https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/. [Accessed: ]
rf:citation
» [Git] 5 Git Tips I Learned Today | Kitco's Today I Learned | Sciencx | https://www.scien.cx/2025/03/02/git-5-git-tips-i-learned-today/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.