Git is the most popular version control system used by developers. Whether you’re new or need a quick refresher, here are the fundamental Git commands to boost your productivity.
1. Check Git Version
Verify the installed Git version:
git --version
2. Configure Git (Username & Email)
Set your user name and email for commits:
git config --global user.name "Your Name"
git config --global user.email "Your Mail"
3. Initialize a New Git Repository
Create a new Git repo in your current folder:
git init
4. Clone a Remote Repository
Download a remote repo to your machine:
git clone https://github.com/user/repo.git
5. Check Repository Status
See changed files and staging status:
git status
6. Stage Files for Commit
Add a specific file to staging area:
git add <filename>
Add all changes:
git add .
7. Commit Changes
Commit staged files with a message:
git commit -m "Your descriptive commit message"
8. View Commit History
Show commit logs:
git log
9. Create a New Branch
Create a branch named feature-branch:
git branch feature-branch
10. Switch to a Branch
Switch to the branch:
git checkout feature-branch
Or create & switch in one command (Git 2.23+):
git switch -c feature-branch
11. Merge Branches
Merge feature-branch into current branch:
git merge feature-branch
12. Push Changes to Remote
Push your commits to the remote repository:
git push origin <branch-name>
13. Pull Latest Changes from Remote
Fetch and merge changes from remote:
git pull origin <branch-name>
14. Delete a Branch
Delete a local branch:
git branch -d <branch-name>
15. Undo Last Commit (Keep Changes)
Undo last commit but keep changes staged:
git reset --soft HEAD~1
Top comments (0)