Essential Git Commands

1. git init

Used to initialize a new Git repository in your current directory. It creates a hidden .git folder where all the metadata is stored.

git init

2. git clone

Creates a local copy of a remote repository. Perfect for contributing to existing projects.

git clone <repository-url>

3. git status

Shows the status of changes as untracked, modified, or staged. Helps you know what’s going on.

git status

4. git add

Adds files to the staging area before committing. You can add individual files or all changes.

git add <filename>
git add .

5. git commit

Creates a snapshot of the staged changes with a message. It’s like saving your project.

git commit -m "your message"

6. git push

Sends your commits from your local repo to a remote one like GitHub.

git push origin main

7. git pull

Fetches and integrates changes from a remote repo into your current branch.

git pull

8. git branch

Lists all branches or creates a new one. Great for working on different features independently.

git branch
git branch <branch-name>

9. git checkout

Switches between branches or restores working directory files. Useful for navigating versions.

git checkout <branch-name>

10. git merge

Merges the changes from another branch into your current branch. Commonly used in feature integrations.

git merge <branch-name>

11. git reset

Resets the current HEAD to the specified state.

git reset --hard HEAD~1

12. git config

Configures Git settings like username and email globally or locally.

git config --global user.name "Your Name"

13. git remote -v

Lists remote connections for your repository.

git remote -v

14. git fetch

Downloads commits, files, and refs from a remote repository.

git fetch

15. git log

Displays the commit history of the current branch.

git log

16. git diff

Shows changes between commits, commit and working tree, etc.

git diff

17. git rm

Deletes tracked files from the working directory and stages the removal.

git rm <filename>

18. git mv

Moves or renames a file or directory.

git mv oldname.txt newname.txt

19. git stash

Saves your changes temporarily without committing them.

git stash

20. git stash pop

Applies the most recently stashed changes and removes them from the stash list.

git stash pop

21. git tag

Tags a specific commit, useful for marking release points.

git tag v1.0

22. git cherry-pick

Applies the changes from a specific commit to the current branch.

git cherry-pick <commit-hash>

23. git revert

Reverts a previous commit by creating a new one.

git revert <commit-hash>

24. git clean

Removes untracked files from your working directory.

git clean -f

25. git blame

Shows who made each change to a file and when.

git blame <file>

26. git shortlog

Summarizes git log output by author.

git shortlog

27. git show

Displays information about a commit, tag, or other git object.

git show <commit-hash>

28. git archive

Creates a tar or zip archive of files in the repo.

git archive --format=zip HEAD > latest.zip

29. git rebase

Reapplies commits on top of another base tip.

git rebase main

30. git reflog

Shows the history of all HEAD changes.

git reflog