Git CLI Documentation
Introduction
Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to collaborate efficiently and maintain a history of changes. This documentation provides an extensive guide to using Git via the command line interface (CLI).
Installation
Windows
- Download the Git installer from the official website: https://git-scm.com/downloads (opens in a new tab)
- Run the installer and follow the setup instructions.
- Choose a preferred terminal emulator (Git Bash is recommended).
- Complete the installation and verify it using:
git --version
Alternative installation methods:
- Using Chocolatey:
choco install git
- Using Scoop:
scoop install git
- Using Git for Windows:
winget install Git.Git
Basic Configuration
After installing Git, configure it with your user details:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To check your configuration:
git config --list
Git Workflow
Initializing a Repository
To start tracking a project with Git:
git init
Cloning a Repository
To clone an existing repository:
git clone <repository-url>
Staging and Committing Changes
- Add files to the staging area:
To add all files:
git add <file>
git add .
- Commit the changes:
git commit -m "Commit message"
Checking Repository Status
To check the current status of your repository:
git status
Viewing Commit History
To see the commit history:
git log
Branching and Merging
Creating a New Branch
git branch <branch-name>
Switching to a Branch
git checkout <branch-name>
Or using the newer command:
git switch <branch-name>
Creating and Switching to a New Branch
git checkout -b <branch-name>
Or:
git switch -c <branch-name>
Merging Branches
Switch to the branch you want to merge into (e.g., main
):
git checkout main
Then merge the other branch:
git merge <branch-name>
Remote Repositories
Adding a Remote Repository
git remote add origin <repository-url>
Pushing Changes
git push origin <branch-name>
Pulling Changes
git pull origin <branch-name>
Fetching Changes Without Merging
git fetch origin
Undoing Changes
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
Undo Last Commit (Discard Changes)
git reset --hard HEAD~1
Remove File from Staging Area
git reset <file>
Reverting a Commit
git revert <commit-hash>
Working with Tags
Creating a Tag
git tag -a <tag-name> -m "Tag message"
Listing Tags
git tag
Pushing Tags to Remote
git push origin <tag-name>
Deleting a Tag
git tag -d <tag-name>
Stashing Changes
If you need to save your work temporarily:
git stash
To apply the stashed changes:
git stash pop
Git Aliases
To create a shortcut for frequently used commands:
git config --global alias.st status
git config --global alias.co checkout
Conclusion
This documentation covers the essential Git CLI commands. For more details, refer to the official Git documentation: https://git-scm.com/doc (opens in a new tab).