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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

12. git pull: Fetch and Merge Changes

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

git pull
Enter fullscreen mode Exit fullscreen mode

13. git push: Push Changes to Remote Repository

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

git push
Enter fullscreen mode Exit fullscreen mode

14. git remote: List Remote Repositories

List the remote repositories connected to your local repository.

git remote
Enter fullscreen mode Exit fullscreen mode

15. git remote -v: Show Remote Repository Details

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

git remote -v
Enter fullscreen mode Exit fullscreen mode

16. git fetch [remote]: Fetch Remote Changes

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

git fetch [remote]
Enter fullscreen mode Exit fullscreen mode

17. git reset [file]: Unstage Changes

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

git reset [file]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

21. git stash: Temporarily Save Changes

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

git stash
Enter fullscreen mode Exit fullscreen mode

22. git stash pop: Apply the Most Recent Stash

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

git stash pop
Enter fullscreen mode Exit fullscreen mode

23. git diff: Show Differences

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

git diff
Enter fullscreen mode Exit fullscreen mode

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

Add a new remote repository to your Git configuration.

git remote add [name] [URL]
Enter fullscreen mode Exit fullscreen mode

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

Remove a remote repository from your configuration.

git remote remove [name]
Enter fullscreen mode Exit fullscreen mode

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

Fetch and merge changes from a specific remote branch.

git pull [remote] [branch]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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

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


git checkout -- [file]
Enter fullscreen mode Exit fullscreen mode

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

Set your Git username globally.

git config --global user.name "[Your Name]"
Enter fullscreen mode Exit fullscreen mode

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]"
Enter fullscreen mode Exit fullscreen mode

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)