10 Jun 2025 - Prajeet Shrestha

Git is a distributed version control system that helps you track changes in your code and collaborate with others. This guide will introduce you to the basics of Git and get you started with essential commands.

Basic Commands

  1. Initialize Git Repository
    git init
  2. Check status of your files in the working directory
    git status
  3. Add files to staging area
    git add <file>
  4. Commit changes with a message
    git commit -m "message"
  5. View commit history
    git log
  6. Create a new branch
    git branch <branch-name>
  7. Switch to a branch
    git checkout <branch-name>
  8. Merge a branch into the current branch
    git merge <branch-name>
  9. Push changes to a remote repository
    git push origin <branch-name>
  10. Pull changes from a remote repository
    git pull origin <branch-name>
  11. Clone a remote repository
    git clone <repository-url>
  12. Remove a file from the staging area
    git rm <file>
  13. View differences between staged and unstaged changes
    git diff
  14. View differences between the last commit and the current state
    git diff HEAD
  15. Reset the staging area to the last commit
    git reset
  16. Discard changes in the working directory
    git checkout -- <file>
  17. View the current branch
    git branch --show-current
  18. View all branches
    git branch
  19. Delete a branch
    git branch -d <branch-name>
  20. Force delete a branch
    git branch -D <branch-name>
  21. View remote repositories
    git remote -v
  22. Add a remote repository
    git remote add <name> <repository-url>
  23. Remove a remote repository
    git remote remove <name>
  24. Fetch changes from a remote repository
    git fetch <remote-name>
  25. View the status of the remote repository
    git remote show <remote-name>
  26. Rename a branch
    git branch -m <old> <new>
  27. Stash changes temporarily
    git stash
  28. Apply stashed changes
    git stash apply
  29. List stashed changes
    git stash list
  30. Drop a specific stash
    git stash drop <stash@{index}>
  31. Clear all stashes
    git stash clear
  32. View the configuration settings
    git config --list
  33. Set a global configuration setting
    git config --global <setting> <value>
  34. Set a local configuration setting
    git config --local <setting> <value>
  35. Show the current configuration file
    git config --edit
  36. Show the current user name
    git config user.name
  37. Show the current user email
    git config user.email
  38. Set the user name
    git config --global user.name "Your Name"
  39. Set the user email
    git config --global user.email "[email protected]"

Note: All these commands should be run in the terminal or command prompt within your project directory.

Understanding the .gitignore

A .gitignore file tells Git which files or directories to ignore in a project. This is useful for excluding files that do not need to be tracked, such as build artifacts, log files, or sensitive information.

How to Use .gitignore

  1. Create a .gitignore file in the root of your repository.
  2. List the files, directories, or patterns you want Git to ignore.
  3. Example:

# Ignore node_modules folder
node_modules/

# Ignore all .log files
*.log

# Ignore environment files
.env
    

Why Use .gitignore?

  • Keeps your repository clean by excluding unnecessary files.
  • Prevents sensitive data from being accidentally committed.
  • Reduces repository size and clutter.

Use SourceTree

SourceTree is a free Git client that provides a graphical interface for managing your Git repositories. It simplifies the process of committing changes, branching, merging, and viewing commit history.
Download SourceTree

SourceTree allows you to perform all the Git commands mentioned above through a user-friendly interface, making it easier for beginners to get started with Git.

For more information, see the official Git documentation on .gitignore.