The Ultimate Git Q&A Guide (Beginner to Expert)

Git is the backbone of modern software development. Whether you are a beginner just learning git init or an advanced user managing complex workflows with git rebase, this Q&A post has you covered.


1. What is core.excludesFile in Git?

Answer:
The core.excludesFile setting in Git defines the global ignore file (similar to .gitignore, but system-wide).

  • By default, .gitignore works per project.
  • core.excludesFile is useful when you want to ignore files across all repositories on your machine (e.g., .DS_Store, IDE configs, log files).

๐Ÿ‘‰ Example:

git config --global core.excludesFile ~/.gitignore_global

Then create a file ~/.gitignore_global and add patterns like:

*.log
.DS_Store
node_modules/


2. What does git branch -f branch-name do?

Answer:
git branch -f force-moves a branch pointer to a specific commit. Itโ€™s useful when you want to reset a branch without checking it out.

๐Ÿ‘‰ Example: Move dev branch to match main:

git branch -f dev main

Now dev points exactly where main is.

โš ๏ธ Be careful โ€” this changes commit history for that branch pointer.


3. How does git stash work?

Answer:
git stash temporarily saves your uncommitted changes (both staged and unstaged) so you can work on something else without committing.

๐Ÿ‘‰ Example:

git stash         # saves changes
git stash list    # see all stashes
git stash pop     # reapply latest stash and remove it
git stash apply   # reapply without removing


4. Difference between git stash pop and git stash apply?

  • git stash pop โ†’ applies the stash and removes it from the stash list.
  • git stash apply โ†’ applies the stash but keeps it in the list.

5. How to use git apply?

Answer:
git apply applies a patch (like a diff) directly to your working directory without committing.

๐Ÿ‘‰ Example:

git diff > fix.patch
git apply fix.patch

This is useful when you want to share changes as patches instead of pushing branches.


6. How to explore history with git log?

Answer:
git log shows commit history. Some useful options:

git log --oneline --graph --decorate --all

  • --oneline โ†’ short hash & message
  • --graph โ†’ ASCII branch graph
  • --decorate โ†’ shows branch/tags
  • --all โ†’ shows all refs

7. How to compare changes with git diff?

Answer:

  • Compare working directory vs. index (unstaged changes): git diff
  • Compare staged changes vs. last commit: git diff --cached
  • Compare two commits: git diff commit1 commit2

8. What is git merge vs. git rebase?

Answer:

  • git merge branchX โ†’ merges branch history, creates a merge commit (keeps history intact).
  • git rebase branchX โ†’ re-applies commits on top of branchX, creating a linear history.

๐Ÿ‘‰ Use merge for shared branches.
๐Ÿ‘‰ Use rebase for private branches before pushing.


9. How does git fetch differ from git pull?

  • git fetch โ†’ downloads changes from remote but does not merge into your branch.
  • git pull = git fetch + git merge (or git rebase if you use --rebase).

10. How to move/rename a file with git mv?

git mv old.txt new.txt
git commit -m "Renamed file"

This keeps history intact instead of manually deleting & re-adding files.


11. What is git pull --rebase?

Answer:
It fetches changes and rebases your commits on top of the updated branch (instead of creating a merge commit).

๐Ÿ‘‰ Example:

git pull --rebase origin main

This keeps history linear.


12. How to use git clean?

Answer:
Removes untracked files (careful โš ๏ธ).

git clean -fd

  • -f โ†’ force
  • -d โ†’ remove untracked dirs

13. How to clone a repo into a subdirectory?

git clone https://github.com/user/repo.git my-subdir

This creates the repo inside my-subdir.


14. How to find who changed a line with git blame?

git blame file.txt

Shows commit hash, author, and time for each line.

๐Ÿ‘‰ Useful with -L to blame specific lines:

git blame -L 10,20 file.txt


15. How to ignore all files except one?

In .gitignore:

*
!file.txt

This ignores everything except file.txt.


16. How to undo a merge commit?

If not pushed yet:

git reset --hard HEAD~1

If pushed, use git revert:

git revert -m 1 <merge_commit_hash>

This undoes merge without rewriting history.


17. What is git switch?

  • git switch branch โ†’ switches branches.
  • git switch -c new-branch โ†’ creates & switches to new branch.

๐Ÿ‘‰ Itโ€™s a modern alternative to git checkout (for branches).


18. What are Git Hooks?

Answer:
Git hooks are scripts that run automatically on certain events (like commits, pushes).

Examples in .git/hooks/:

  • pre-commit โ†’ run tests before committing
  • pre-push โ†’ check code quality before pushing
  • commit-msg โ†’ enforce commit message rules

๐Ÿ‘‰ Hooks make Git workflows automated & consistent.


๐Ÿš€ Final Thoughts

This Q&A covered everything from basic commands like git stash to advanced tricks like core.excludesFile, undoing merges, and Git hooks. Bookmark this page, and youโ€™ll never get stuck in Git again.

๐Ÿ‘‰ Share this with your dev friends. Every developer has been stuck with Git at least once ๐Ÿ˜‰


npm install not working? Hereโ€™s How to Fix Common npm Errors (2025 Guide)

Introduction

If youโ€™ve worked with Node.js, youโ€™ve probably run into the dreaded npm install errors. Whether itโ€™s ELIFECYCLE, dependency conflicts, or permission issues, these errors always seem to pop up when youโ€™re in a hurry.

The good news? Most of these problems have simple fixes.

In this 2025 updated guide, Iโ€™ll walk you through the most common npm errors developers face today and show you step-by-step solutions. Bookmark this page โ€” itโ€™ll save you hours of frustration in the future!


1. Error: npm ERR! code ELIFECYCLE

What It Looks Like

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! some-package@1.0.0 start: `node index.js`
npm ERR! Exit status 1

Why It Happens

This means the package youโ€™re installing or running has a script that failed (for example, during npm install or npm start).

How to Fix It

โœ… Step 1: Clean npm cache

npm cache clean --force

โœ… Step 2: Delete node_modules and reinstall

rm -rf node_modules package-lock.json
npm install

โœ… Step 3: Check Node.js and npm versions

node -v
npm -v

Update if necessary:

npm install -g npm@latest
nvm install <version>

โœ… Step 4: Run with verbose flag to debug

npm install --verbose


2. Error: npm ERR! ERESOLVE unable to resolve dependency tree

What It Looks Like

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree

Why It Happens

This happens when two or more packages require incompatible versions of the same dependency.

How to Fix It

โœ… Step 1: Use legacy peer deps

npm install --legacy-peer-deps

โœ… Step 2: Force install (not always recommended)

npm install --force

โœ… Step 3: Manually resolve versions
Open package.json and adjust the conflicting versions. Then reinstall:

rm -rf node_modules package-lock.json
npm install


3. Error: EACCES: permission denied

What It Looks Like

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

Why It Happens

This usually happens on Linux or macOS when npm doesnโ€™t have the correct permissions to install global packages.

How to Fix It

โœ… Step 1: Avoid using sudo with npm (bad practice).
โœ… Step 2: Change npmโ€™s default directory:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

โœ… Step 3: Add the new path to your environment:

export PATH=~/.npm-global/bin:$PATH

โœ… Step 4: Update your shell config (.bashrc, .zshrc, etc.) with the new path.


4. Error: โ€œGlobal package not foundโ€

What It Looks Like

command not found: nodemon

Why It Happens

You installed a global package but your system canโ€™t find it.

How to Fix It

โœ… Step 1: Check global npm directory

npm list -g --depth=0

โœ… Step 2: Ensure itโ€™s in your PATH

export PATH=$(npm config get prefix)/bin:$PATH

โœ… Step 3: Reinstall the package globally

npm install -g nodemon


5. Error: โ€œnpm command not foundโ€

What It Looks Like

bash: npm: command not found

Why It Happens

This means Node.js and npm arenโ€™t properly installed or PATH isnโ€™t configured.

How to Fix It

โœ… Step 1: Verify installation

node -v
npm -v

โœ… Step 2: Reinstall Node.js (recommended via nvm)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
nvm install --lts

โœ… Step 3: Restart terminal and try again.


6. Best Practices to Avoid npm Errors

  • Always use the latest LTS version of Node.js.
  • Delete node_modules and package-lock.json when facing weird issues.
  • Use npx instead of installing packages globally.
  • Keep your dependencies updated:
npm outdated
npm update

  • Consider alternatives like yarn or pnpm if your project supports them.

Conclusion

npm errors can be frustrating, but with the right fixes, you can resolve most issues in minutes.

๐Ÿ‘‰ Save this page for future reference, and if you found it helpful, share it with your fellow developers.

Happy coding! ๐Ÿš€

How to find what packages will be updated in package.json file ?

There is an npm command called npm outdated, which will tell us what packages will be updated.

Before running npm update command which will help us to updated packages, it is always better to run npm outdated first. So it will give you you list of packages with current version, wanted version and latest version.

Whenever we run npm update all packages will be updated to wanted versions which are listed when we run npm update command.

For Example, If you want to update a lodash package to its latest version we need use the following command.

After completion of installation, run the following command to check which version is installed for lodash.

How to publish a package to npm ?

To publish a package to npm, we should follow the following steps.

STEP 1 : Make sure your project have package.json file something like this.

STEP 2 : update your entry file with required functionality. Here entry file is index.js as we defined in package.json as “main”: “index.js”. Following is some example code.

STEP 3: signup to npm using https://www.npmjs.com/signup

STEP 4: Now login to npm using the following command in the terminal

STEP 5: publish the package using the following command. (Make sure package name is unique)

Finally you are able use that package anywhere by just installing with the following command

Showing Alert For Page Refresh , Closing tab or Window If Unsaved changes are there in the Form – ReactJS

If unsaved changes are there in the form, we will use the following code to show default alert message when user tries to reload or close the window or close the tab.