ASHU
Git

Git Commands Every Developer Should Know

12 min read

1. git init

Initializes a new Git repository.

  • Converts a normal folder into a Git repo
  • Creates a hidden .git folder (history, branches, config)
  • Use when starting a new project

After this, Git starts tracking file changes.


2. git config

Sets your identity for commits.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  • --global → applies to all projects
  • Without --global → applies to current project only
  • Required so Git knows who made which change

3. git clone

Copies an existing remote repository to your system.

git clone https://github.com/user/project.git
  • Downloads full code + history
  • Automatically connects to the remote repo

4. git remote

Manages remote repositories (GitHub, GitLab, etc.).

Check remotes:

git remote -v

Add a remote:

git remote add origin https://github.com/user/project.git

origin is just a common name for the main remote.


5. git status

Shows the current state of your project.

git status

Tells you:

  • Modified files
  • Staged files
  • Untracked files

Run this before every commit.


6. git add

Stages changes for commit.

git add filename
git add .
  • Git only commits staged files
  • Think of it as "selecting files for the snapshot"

7. git commit

Saves a snapshot of staged changes.

git commit -m "Add login feature"

Quick commit for tracked files:

git commit -a -m "Fix bug"

Edit last commit:

git commit --amend

⚠️ Commits are local only until pushed.


8. git push

Uploads local commits to remote repository.

git push origin main
  • Shares your work with others
  • Usually done on feature branches (not directly on main)

9. git pull

Updates your local branch with remote changes.

git pull origin main
  • Fetches + merges changes
  • Use before pushing to avoid conflicts

10. git fetch

Downloads remote changes without merging.

git fetch origin

View new commits:

git log HEAD..origin/main

Best when you want to review first, merge later.


11. git branch

Creates and lists branches.

Create branch:

git branch new-feature

List branches:

git branch

* shows current branch.


12. git checkout

Switches branches or restores files.

Switch branch:

git checkout new-feature

Create & switch:

git checkout -b new-feature

Restore file:

git checkout -- style.css

Modern alternative:

git switch new-feature

13. git merge

Combines one branch into another.

git merge new-feature
  • Automatic if no conflicts
  • Manual fix required if conflicts occur

After fixing conflicts:

git add .
git commit

14. git rebase

Rewrites commit history for a clean timeline.

git rebase main
  • Creates a linear history
  • ❌ Do NOT rebase shared branches

15. git log

Shows commit history.

git log
git log --oneline
git log --graph

Useful for:

  • Debugging
  • Code review
  • Understanding project evolution

16. git diff

Shows changes line-by-line.

git diff
git diff branch1..branch2

Use before committing to see exact changes.


17. git stash

Temporarily saves uncommitted work.

git stash
git stash apply
git stash pop

Useful when switching branches mid-work.


18. git reset

Moves HEAD to a previous commit.

Soft (keep staged):

git reset --soft HEAD~1

Mixed (unstage changes):

git reset HEAD~1

Hard (delete everything):

git reset --hard HEAD~1

⚠️ Hard reset permanently deletes work.


19. git revert

Safely undo a pushed commit.

git revert <commit-hash>
  • Creates a new commit
  • Does NOT rewrite history
  • Best for shared branches

20. git cherry-pick

Apply a specific commit from another branch.

git cherry-pick <commit-hash>
  • Useful for copying bug fixes
  • Creates a new commit with a new hash