DEV Community

Cover image for Git Essentials for Beginners: A Comprehensive Guide to Git Commands
Shubhadip Bhowmik
Shubhadip Bhowmik

Posted on

Git Essentials for Beginners: A Comprehensive Guide to Git Commands

Are you new to the world of version control and looking to dive into Git? Congratulations! You're about to embark on a journey that will greatly enhance your ability to collaborate on software projects. In this blog post, we'll cover the essential Git commands every newbie should know to get started with version control like a pro.

Why Git?

Git is a distributed version control system widely used in the software development industry. It allows you to track changes in your codebase, collaborate with others seamlessly, and maintain a history of your project's evolution. Whether you're working on a solo project or part of a team, Git is an indispensable tool.
Let's Get Started: Essential Git Commands

1. git init: Initialize a New Repository

The first step in using Git is to create a Git repository in your project directory. This initializes Git's tracking system.

git init
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

2. git clone [repository URL]: Clone a Remote Repository

To work with an existing Git repository from a remote source (like GitHub or GitLab), use the git clone command.

git clone [repository URL]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

3. git add [file or directory]: Stage Changes

Before you commit changes, you need to stage them. The git add command lets you select which files or directories to include in your next commit.

git add [file or directory]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

4. git commit -m "[commit message]": Commit Changes

Commits are like snapshots of your project. Use git commit to save your staged changes with a descriptive message.

git commit -m "Initial commit"
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

5. git status: Check Repository Status

Want to know what's happening in your Git repository? git status provides information about untracked files and changes ready for commit.

git status
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

6. git log: View Commit History

You can view a log of all commits in your repository using git log. This command provides detailed information about each commit, including commit messages and authors.

git log
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

7. git branch: List Branches

Branches allow you to work on different features or versions of your project simultaneously. git branch lists all branches in your repository.

git branch
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

8. git branch [branch name]: Create a New Branch

Create a new branch to work on a specific task or feature. Use the git branch [branch name] command.


git branch [branch name]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

9. git checkout [branch name]: Switch to a Branch

Switch between branches with git checkout. This command helps you focus on specific tasks without affecting the main project.

git checkout [branch name]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

10. git checkout -b [new branch name]: Create and Switch to a New Branch

Simplify branch creation and switching with the -b flag.

git checkout -b [new branch name]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

11. git merge [branch name]: Merge Branches

Merging combines changes from one branch into another. Use git merge to integrate your work.

git merge [branch name]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

12. git pull: Fetch and Merge Changes

Keep your local branch up to date with remote changes using git pull.

git pull
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

13. git push: Push Changes to Remote Repository

Share your work with others by pushing your local changes to the remote repository.

git push
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

14. git remote: List Remote Repositories

List the remote repositories connected to your local repository.

git remote
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

15. git remote -v: Show Remote Repository Details

Get detailed information about your remote repositories, including their URLs.

git remote -v
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

16. git fetch [remote]: Fetch Remote Changes

Fetch changes from a remote repository without merging them into your current branch.

git fetch [remote]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

17. git reset [file]: Unstage Changes

If you've mistakenly staged a file, use git reset to unstage it.

git reset [file]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

18. git reset --soft [commit]: Reset to a Previous Commit (Soft)

Move the branch pointer to a previous commit while keeping your changes staged.

git reset --soft [commit]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

19. git reset --mixed [commit]: Reset to a Previous Commit (Mixed)

Move the branch pointer to a previous commit and unstage your changes.

git reset --mixed [commit]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

20. git reset --hard [commit]: Reset to a Previous Commit (Hard)

Move the branch pointer to a previous commit and discard all changes.

git reset --hard [commit]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

21. git stash: Temporarily Save Changes

Stash changes when you need to switch branches or work on something else temporarily.

git stash
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

22. git stash pop: Apply the Most Recent Stash

Retrieve your stashed changes and remove them from the stash list.

git stash pop
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

23. git diff: Show Differences

Use git diff to see the differences between your working directory and the last commit.

git diff
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

24. git remote add [name] [URL]: Add a Remote Repository

Add a new remote repository to your Git configuration.

git remote add [name] [URL]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

25. git remote remove [name]: Remove a Remote Repository

Remove a remote repository from your configuration.

git remote remove [name]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

26. git pull [remote] [branch]: Pull from a Specific Remote Branch

Fetch and merge changes from a specific remote branch.

git pull [remote] [branch]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

27. git push [remote] [branch]: Push to a Specific Remote Branch

Push your local branch to a specific branch on the remote repository.

git push [remote] [branch]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

28. git checkout -- [file]: Discard Changes in a File

Revert a specific file to the last committed state, discarding any changes.


git checkout -- [file]
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

29. git config --global user.name "[Your Name]": Set Global Git Username

Set your Git username globally.

git config --global user.name "[Your Name]"
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

30. git config --global user.email "[your@email.com]": Set Global Git Email

Set your Git email address globally.

git config --global user.email "[your@email.com]"
💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด 💬 ชาวเน็ตแห่แชร์: shubhadip bhowmik/git essentials for beginners a comprehensive guide to git commands cc - สรุปดราม่าล่าสุด

Congratulations, you've just taken your first steps into the exciting world of Git! These essential Git commands will help you manage your code, collaborate with others, and track changes effectively. As you become more comfortable with Git.

Thanks for reading my blog!

Let's connect with each other. Here's my LinkedIn and Twitter. Feel free to reach out, and let's continue the discussion. 🙌

Top comments (0)