WE CODE NOW
  • Home 
  • Blog 
  • Guides 
Guides
  1. Home
  2. Guides
  3. Git
  4. Essential Git Commands Every Developer Should Know

Essential Git Commands Every Developer Should Know

Posted on June 7, 2024 • 3 min read • 563 words
Git
 
Version Control
 
Commands
 
Git
 
Version Control
 
Commands
 
Share via

Discover the essential Git commands that every developer needs to master. Learn how to initialize a repository, manage branches, and more.

On this page
  • Setup and Configuration
  • Basic Operations
  • Branching and Merging
  • Stashing
  • Remote Operations
  • Reset and Revert
  • Advanced Tips

Essential Git Commands Every Developer Should Know  

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.

Setup and Configuration  

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.

Basic Operations  

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.

Branching and Merging  

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.

Stashing  

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.

Remote Operations  

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.

Reset and Revert  

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.

Advanced Tips  

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.

 Git Terminology Explained
Advanced Git Commands for Pro Developers 
On this page:
  • Setup and Configuration
  • Basic Operations
  • Branching and Merging
  • Stashing
  • Remote Operations
  • Reset and Revert
  • Advanced Tips
Copyright © 2024 WE CODE NOW All rights reserved.
WE CODE NOW
Link copied to clipboard
WE CODE NOW
Code copied to clipboard