20 Git Commands Every Developer Should Master

Git is an essential tool for version control, allowing developers to track changes in their codebase and collaborate effectively with others. Whether you're new to Git or looking to enhance your skills, mastering these 20 Git commands can significantly improve your workflow and make you a version control pro.

1. git init

Purpose: Initializes a new Git repository in your project directory.

git init

Example: Run this command in a project folder to start tracking changes with Git.

2. git clone

Purpose: Creates a local copy of a remote repository.

git clone [repository_url]

Example: git clone https://github.com/username/repository.git will clone the repository to your local machine.

3. git add

Purpose: Stages changes for the next commit.

git add [file_name]   # To stage a specific file
git add .             # To stage all changes

Example: git add . stages all modified and new files in the current directory.

4. git commit

Purpose: Records changes to the repository.

git commit -m "your commit message"

Example: git commit -m "Initial commit" creates a commit with a message describing the changes.

5. git status

Purpose: Shows the status of the working directory and staging area.

git status

Example: Running git status will show which files are staged, modified, or untracked.

6. git log

Purpose: Displays the commit history.

git log

Example: git log --oneline shows a brief commit history.

7. git branch

Purpose: Lists, creates, or deletes branches.

git branch              # Lists all branches
git branch [branch_name]   # Creates a new branch

Example: git branch feature-branch creates a new branch named 'feature-branch'.

8. git checkout

Purpose: Switches to a different branch or commit.

git checkout [branch_name]   # Switches to the specified branch

Example: git checkout main switches to the 'main' branch.

9. git merge

Purpose: Merges changes from one branch into the current branch.

git merge [branch_name]

Example: git merge feature-branch merges the 'feature-branch' into the current branch.

10. git pull

Purpose: Fetches and integrates changes from a remote repository.

git pull [remote_name] [branch_name]

Example: git pull origin main fetches and merges changes from the remote 'main' branch.

11. git push

Purpose: Uploads local changes to a remote repository.

git push [remote_name] [branch_name]

Example: git push origin main pushes the local 'main' branch to the remote repository.

12. git remote

Purpose: Manages remote repositories.

git remote add [name] [url]   # Adds a new remote
git remote -v                 # Lists all remotes

Example: git remote add origin https://github.com/username/repository.git links a remote repository named 'origin'.

13. git fetch

Purpose: Downloads changes from a remote repository without merging.

git fetch [remote_name]

Example: git fetch origin fetches changes from the 'origin' remote repository.

14. git rebase

Purpose: Reapplies commits on top of another base commit.

git rebase [branch_name]

Example: git rebase main moves the commits from the current branch on top of the 'main' branch.

15. git stash

Purpose: Temporarily saves changes without committing them.

git stash

Example: Use git stash to save changes when you need to switch branches without committing them.

16. git reset

Purpose: Resets the current branch to a specified state.

git reset [commit_hash]

Example: git reset --hard HEAD~1 resets the last commit, discarding changes.

17. git revert

Purpose: Creates a new commit that undoes changes from a previous commit.

git revert [commit_hash]

Example: git revert a1b2c3d reverts the changes made in commit 'a1b2c3d'.

18. git diff

Purpose: Shows the differences between files.

git diff                 # Shows changes in working directory
git diff [branch1] [branch2]   # Compares two branches

Example: git diff main feature-branch compares changes between 'main' and 'feature-branch'.

19. git tag

Purpose: Tags specific points in history as important releases.

git tag [tag_name]
git tag -a [tag_name] -m "message"   # Annotated tag

Example: git tag v1.0.0 creates a tag for version 1.0.0.

20. git cherry-pick

Purpose: Applies the changes from a specific commit.

git cherry-pick [commit_hash]

Example: git cherry-pick a1b2c3d applies changes from commit 'a1b2c3d' to the current branch.

Mastering these Git commands will help you manage your projects more effectively and work seamlessly with teams. Whether it's creating branches, managing remote repositories, or saving changes temporarily, these commands cover all the essential aspects of version control. Happy coding!