DEV Community

Cover image for GIT - Beginner’s Guide: Useful GIT commands you should know
Cezary Mazur
Cezary Mazur

Posted on • Edited on

GIT - Beginner’s Guide: Useful GIT commands you should know

Technology: GIT
Series: Beginner's Guide
Topic: Useful GIT commands you should know
Version: 1.1
Author: Cezary Mazur
Author Website: https://cezarymazur.pl

Version control is a crucial aspect of modern software development, and Git stands out as one of the most popular version control systems. If you're a beginner navigating the vast world of Git, this guide will provide you with essential commands to kickstart your journey. From basic operations to more advanced features, we'll explore the functionalities that will make your development workflow smoother.


Understanding Git Basics

Each command in this article should be executed in a terminal in your project folder.

Initializing a Repository

To start using Git, you need to initialize a repository. This is where Git will track changes in your project.

git init
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository

If you're working on an existing project - for example project with the repository in GitLab, BitBucket, or GitHub - you can clone it to your local machine.

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

Image description


Day-to-Day Commands

Tracking Changes

Once your repository is set up, you'll make changes to your code. The following commands help you track and commit those changes.

git add <filename>
git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Checking Status

It's crucial to know the status of your repository at any given time.

git status
Enter fullscreen mode Exit fullscreen mode

Viewing Commit History

Understanding the commit history is essential for collaboration and debugging.

git log
Enter fullscreen mode Exit fullscreen mode

Collaborating with Others

Pulling Changes

If someone else has made changes to the repository, you need to pull those changes to your local machine.

git pull
Enter fullscreen mode Exit fullscreen mode

Pushing Changes

When you've made changes and want to share them with others, you push your commits.

git push
Enter fullscreen mode Exit fullscreen mode

Branching

Branching allows you to work on new features or bug fixes without affecting the main codebase.

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

Image description


Advanced Git Commands

Merging Branches

Once you've completed your work on a branch, you merge it back into the main branch.

bashCopy code
git merge <branch-name>

Enter fullscreen mode Exit fullscreen mode

Resolving Conflicts

Conflicts may arise when merging branches with conflicting changes. Resolve them using:

git mergetool
Enter fullscreen mode Exit fullscreen mode

Pros and Cons of Git

✅ Pros

  1. Distributed Version Control: Git allows decentralized collaboration, enabling developers to work offline and merge changes seamlessly.
  2. Branching and Merging: The ability to create branches and merge them easily facilitates collaborative development and feature isolation.
  3. Fast and Efficient: Git is designed to be quick, making it ideal for both small and large projects.

😥 Cons

  1. Learning Curve: For beginners, Git's extensive feature set can be overwhelming, leading to a steep learning curve.
  2. Confusing Terminology: Concepts like "rebase" and "detached HEAD" can be confusing for new users.
  3. Storage Size: Repositories with a long history can consume significant disk space.

Image description


Tips for Efficient Git Usage

  1. Regularly Commit: Make small, frequent commits to create a comprehensive commit history.
  2. Use Branches Wisely: Create branches for new features or bug fixes to keep the main branch stable.
  3. Write Meaningful Commit Messages: Clearly articulate the purpose of each commit for better collaboration.

Visualizing Git Concepts

Images can help visualize Git workflows. Consider using icons or diagrams to illustrate concepts like branching, merging, and commit history.


How to learn Git?

Courses

One of the best ways to learn Git is through courses. You can find many of them for free on YouTube or purchase a course on Udemy. Regardless of which course you choose, it will help you better understand and implement the use of GIT.

Games

I highly recommend games that can help you learn Git and its commands:

  1. LearnGitBranching: https://learngitbranching.js.org/
  2. OhMyGit: https://ohmygit.org/

Leaving Comments, Reacting, and Saving the Article

I hope this guide provides you with a solid foundation for using Git. If you have any questions or thoughts, please leave a comment below.

Your feedback is valuable to me! If you found this article helpful, consider reacting to it and saving it for future reference.

Explore More

For further insights and in-depth tutorials, visit author's website.

Stay tuned for more articles on front-end development, and 😀 happy coding!

Top comments (6)

Collapse
 
cezarymazur profile image
Cezary Mazur

At least should be familiar😊

Some comments may only be visible to logged-in visitors. Sign in to view all comments.