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
- Initialize Git Repository
git init
- Check status of your files in the working directory
git status
- Add files to staging area
git add <file>
- Commit changes with a message
git commit -m "message"
- View commit history
git log
- Create a new branch
git branch <branch-name>
- Switch to a branch
git checkout <branch-name>
- Merge a branch into the current branch
git merge <branch-name>
- Push changes to a remote repository
git push origin <branch-name>
- Pull changes from a remote repository
git pull origin <branch-name>
- Clone a remote repository
git clone <repository-url>
- Remove a file from the staging area
git rm <file>
- View differences between staged and unstaged changes
git diff
- View differences between the last commit and the current state
git diff HEAD
- Reset the staging area to the last commit
git reset
- Discard changes in the working directory
git checkout -- <file>
- View the current branch
git branch --show-current
- View all branches
git branch
- Delete a branch
git branch -d <branch-name>
- Force delete a branch
git branch -D <branch-name>
- View remote repositories
git remote -v
- Add a remote repository
git remote add <name> <repository-url>
- Remove a remote repository
git remote remove <name>
- Fetch changes from a remote repository
git fetch <remote-name>
- View the status of the remote repository
git remote show <remote-name>
- Rename a branch
git branch -m <old> <new>
- Stash changes temporarily
git stash
- Apply stashed changes
git stash apply
- List stashed changes
git stash list
- Drop a specific stash
git stash drop <stash@{index}>
- Clear all stashes
git stash clear
- View the configuration settings
git config --list
- Set a global configuration setting
git config --global <setting> <value>
- Set a local configuration setting
git config --local <setting> <value>
- Show the current configuration file
git config --edit
- Show the current user name
git config user.name
- Show the current user email
git config user.email
- Set the user name
git config --global user.name "Your Name"
- 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
- Create a .gitignore file in the root of your repository.
- List the files, directories, or patterns you want Git to ignore.
- 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.