Essential Git Commands Every Developer Should Know
Posted on June 7, 2024 • 3 min read • 563 wordsDiscover the essential Git commands that every developer needs to master. Learn how to initialize a repository, manage branches, and more.
Mastering Git is crucial for any developer. Here’s a quick reference guide to the most essential Git commands that will help you efficiently manage your code.
Set your username and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Personalize your commits with your name and email.
Initialize a new repository:
git init
Kick off a new project by creating a Git repository.
Clone an existing repository:
git clone https://github.com/user/repo.git
Make a copy of an existing repository to start collaborating.
Check repository status:
git status
Keep an eye on the status of your working directory and staging area.
Add files to the staging area:
git add filename
git add .
Stage changes by adding files to the staging area. The staging area is like a clipboard where you prepare changes before committing.
Commit changes:
git commit -m "Commit message"
Commit your staged changes with a descriptive message. This creates a snapshot of your project’s history.
View commit history:
git log
Dive into the history of your project with a detailed log of commits.
Create a new branch:
git branch branch-name
Branching allows you to create a separate line of development for new features or fixes.
Switch to a branch:
git checkout branch-name
Jump between branches to switch contexts.
Create and switch to a new branch:
git checkout -b branch-name
Streamline your workflow by creating and switching to a new branch in one go.
Merge a branch:
git checkout main
git merge branch-name
Combine the changes from another branch into your current branch. Perfect for bringing new features into the main line of development.
Rebase a branch:
git checkout branch-name
git rebase main
Rebase your branch to maintain a linear project history. This reapplies your changes on top of another branch.
Stash changes:
git stash
Temporarily save your changes without committing them. Great for when you need to switch contexts quickly.
List stashes:
git stash list
View all stashed changes to keep track of your work.
Apply stash:
git stash apply
Retrieve your stashed changes without removing them from the stash list.
Pop stash:
git stash pop
Apply and remove your most recent stash in one command.
Push changes to remote:
git push origin branch-name
Send your commits to a remote repository to share your work with others.
Pull changes from remote:
git pull origin branch-name
Fetch and integrate changes from a remote repository into your current branch. Stay up-to-date with your team.
Soft reset:
git reset --soft HEAD~1
Move the HEAD pointer to a previous commit, but keep changes in the staging area. Perfect for amending your last commit.
Hard reset:
git reset --hard HEAD~1
Reset your repository to a previous state and discard all changes in the working directory.
Revert a commit:
git revert commit-hash
Undo changes from a specific commit by creating a new commit. This is a safe way to roll back changes.
Interactive rebase:
git rebase -i HEAD~n
Edit, reorder, and squash commits interactively to clean up your commit history.
Cherry-pick commits:
git cherry-pick commit-hash
Apply changes from specific commits to your current branch. Handy for bringing over individual changes without merging whole branches.
View changes:
git diff
See the differences between the working directory and the staging area, or between two commits.
Clean untracked files:
git clean -f
Remove untracked files from your working directory to keep things tidy.