Git is a distributed version control system that helps you track changes in your code, collaborate with others, and manage different versions of your projects. This guide covers the essential Git commands every developer should know.
Git allows you to:
- Track changes in your files over time
- Collaborate with multiple developers on the same project
- Create branches to work on different features simultaneously
- Merge changes from different contributors
- Revert to previous versions when needed
The typical Git workflow follows these steps:
- Initialize or clone a repository
- Add changes to the staging area
- Commit changes with a descriptive message
- Push changes to a remote repository (like GitHub)
Creates a new Git repository in the current directory.
git initDownloads a repository from a remote source (like GitHub) to your local machine.
git clone https://github.com/username/repository-name.gitShows the current state of your working directory and staging area.
git statusStages changes for commit. You can add specific files or all changes.
git add filename.txt # Add specific file
git add . # Add all changes
git add *.js # Add all JavaScript filesSaves staged changes to the repository with a descriptive message.
git commit -m "Add user authentication feature"
git commit -am "Fix bug in login system" # Add and commit in one stepUploads your local commits to a remote repository.
git push origin main # Push to main branch
git push origin feature-name # Push to specific branchDownloads and merges changes from a remote repository to your local branch.
git pull origin mainShows the commit history.
git log # Full commit history
git log --oneline # Condensed view
git log --graph # Visual branch representationShows differences between versions.
git diff # Changes in working directory
git diff --staged # Changes in staging area
git diff HEAD~1 # Compare with previous commitDisplays information about a specific commit.
git show commit-hash
git show HEAD # Show latest commitLists, creates, or deletes branches.
git branch # List all branches
git branch feature-login # Create new branch
git branch -d feature-login # Delete branchSwitches between branches or commits.
git checkout main # Switch to main branch
git checkout -b new-feature # Create and switch to new branch
git switch main # Modern alternative to checkout
git switch -c new-feature # Create and switch (modern syntax)Combines changes from one branch into another.
git checkout main
git merge feature-branchManages remote repository connections.
git remote -v # View remote repositories
git remote add origin https://github.com/username/repo.git
git remote remove origin # Remove remote connectionDownloads changes from remote repository without merging.
git fetch origin
git fetch --all # Fetch from all remotesUnstages changes or moves the branch pointer.
git reset filename.txt # Unstage specific file
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --hard HEAD~1 # Undo last commit, discard changesCreates a new commit that undoes previous changes.
git revert commit-hash # Safely undo specific commitDiscards changes in working directory.
git checkout -- filename.txt # Discard changes to specific file
git checkout . # Discard all changesTemporarily saves changes without committing.
git stash # Stash current changes
git stash pop # Apply and remove latest stash
git stash list # View all stashes
git stash apply stash@{0} # Apply specific stash- Create a new branch for your feature
- Make changes and commit them
- Push the branch to remote
- Create a pull request
- Merge after review
git checkout -b feature/user-profile
# Make your changes
git add .
git commit -m "Add user profile page"
git push origin feature/user-profile- Pull latest changes from main
- Create feature branch
- Work on your changes
- Push and create pull request
- Merge after review
git pull origin main
git checkout -b feature/new-component
# Work on your feature
git add .
git commit -m "Implement new component"
git push origin feature/new-component- Use present tense ("Add feature" not "Added feature")
- Keep the first line under 50 characters
- Use imperative mood ("Fix bug" not "Fixes bug")
- Be descriptive but concise
Add user authentication system
Fix memory leak in image processing
Update README with installation instructions
Remove deprecated API endpoints
- Commit frequently with small, logical changes
- Always pull before starting new work
- Use branches for features and bug fixes
- Test your code before committing
- Write meaningful commit messages
- Use
.gitignoreto exclude unnecessary files
git help <command> # Get help for specific command
git <command> --help # Alternative help syntax
git help # General helpSimply, combines all the relevant selected commits into a single commit
Remember, Git might seem overwhelming at first, but mastering these basic commands will make you much more productive as a developer. Start with the basics and gradually incorporate more advanced features as you become comfortable!