Version control is an essential part of every developer’s workflow. Recently, I learned how to use Git and GitHub, and I wanted to share the basic commands I came across and their uses in simple terms.
Let’s dive in!
📋 Getting Started
git init
Initializes a new Git repository in your project directory.git status
Displays the status of your working directory and staging area, showing changes tracked and untracked by Git.git add <file>
Stages changes for the next commit. Usegit add .
to stage all changes.git commit -m "message"
Records changes to the repository with a descriptive message.
🌐 Working with Remote Repositories
git remote add origin <url>
Links your local repository to a remote repository hosted online.git remote -v
Displays the URLs of the remote repositories you’ve added.git push
Uploads your local commits to a remote repository (e.g., GitHub).git pull
Fetches and merges changes from a remote repository into your local branch.git fetch
Fetches changes from a remote repository without merging them.git clone <url>
Creates a local copy of a remote repository.
🔄 Branch Management
git branch
Lists all branches in your repository.git branch -m <new-branch-name>
Renames the current branch.git branch -d <branch-name>
Deletes the specified branch.git switch <branch-name>
Switches to the specified branch.git switch -c <new-branch-name>
Creates and switches to a new branch.git merge <branch-name>
Combines changes from the specified branch into the current branch.
🧰 Other Useful Commands
git stash
Temporarily stores uncommitted changes so you can work on something else.git stash pop
Reapplies the most recent stash and removes it from the stash list.git diff
Shows differences between your working directory and the index or between commits.git rebase
Reapplies commits on top of another branch, helping to maintain a clean commit history.git reflog
Displays a log of all reference updates, like branch checkouts and commits.
✏️ Managing Files
.gitignore
Specifies files and directories for Git to ignore.gitkeep
Used to track empty directories in a Git repository, as Git doesn’t track empty folders by default.
Wrapping Up
These commands are the backbone of version control with Git and GitHub. As you practice, you’ll discover how they simplify collaboration and improve code management.
What’s your favorite Git command? Share it below!