DEV Community

Mohamed Ibrahim
Mohamed Ibrahim

Posted on

Learning Git and GitHub: The 10 Most Common Commands Explained 🌟

If you’re a developer, understanding Git and GitHub is non-negotiable. They’re essential tools for version control and collaboration, making it easier to manage your codebase and work with a team.

In this article, I’ll walk you through the 10 most common Git commands you’ll use daily. Whether you’re just getting started or need a quick refresher, this guide has got you covered! 🚀

1. git init 🛠️

This command initializes a new Git repository in your project. It creates a .git folder to track changes.

git init
Enter fullscreen mode Exit fullscreen mode

Tip: Use this only for starting a new repository. If you’re cloning an existing one, skip to git clone. 🔑

2. git clone 🖇️

Used to copy an existing repository to your local machine.

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Example: 💡

git clone https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

3. git status 🔍

Check the current status of your repository, including untracked, staged, and modified files.

git status
Enter fullscreen mode Exit fullscreen mode

This command is your best friend for staying updated on your project’s state!

4. git add ➕

Stage changes to prepare them for a commit.

git add <file>
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use git add . to stage all changes in the current directory.🔑

5. git commit 💾

Save your staged changes with a meaningful message.

git commit -m "Your commit message here"
Enter fullscreen mode Exit fullscreen mode

Example: 💡

git commit -m "Fixed a bug in the login form"
Enter fullscreen mode Exit fullscreen mode

6. git push ⬆️

Send your local changes to a remote repository.

git push origin <branch>
Enter fullscreen mode Exit fullscreen mode

Common Usage: Push to the main branch. 🔑

git push origin main
Enter fullscreen mode Exit fullscreen mode

7. git pull ⬇️

Fetch and merge changes from a remote repository to your local machine.

git pull origin <branch>
Enter fullscreen mode Exit fullscreen mode

Use Case: Keep your local branch updated with the latest changes from main.💡

8. git branch 🌳

List, create, or delete branches in your repository.

git branch
Enter fullscreen mode Exit fullscreen mode

To Create a New Branch: 🔑

git branch <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

9. git checkout 🔄

Switch between branches or restore files.

git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use git switch as a modern alternative for switching branches. 🔑

10. git merge 🔗

Combine changes from one branch into another.

git merge <branch-to-merge>
Enter fullscreen mode Exit fullscreen mode

Example: Merge feature branch into main: 💡

git checkout main  
git merge feature
Enter fullscreen mode Exit fullscreen mode

🛠️ Additional Commands to Explore

  • git log: View commit history.
  • git reset: Unstage changes or reset commits.
  • git stash: Save changes for later without committing.

📖 Conclusion

Mastering these 10 commands will give you a solid foundation for working with Git and GitHub. Remember, Git is all about practice. The more you use these commands, the more natural they’ll feel.

Follow Me

Thank you for reading my blog. 🚀 You can follow me on GitHub and connect on Twitter

If you found this guide helpful, share it with your friends or leave a comment below. Let’s keep learning together! 💪

Top comments (0)