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

Cezary Mazur @cezarymazur

About: AI Frontend Trainer & Consultant Consulting — Mentoring — Workshops Helping companies improve frontend processes. Transforming teams into AI-powered innovators.

Location:
Szczecin, Poland
Joined:
Jun 23, 2023

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

Publish Date: Dec 24 '23
13 6

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!

Comments 6 total

  • Cezary Mazur
    Cezary MazurDec 24, 2023

    At least should be familiar😊

  • William Torrez
    William TorrezDec 24, 2023

    What happen with my git?
    I make git push origin master

    fatal: 'origin' does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

    • Idris
      IdrisDec 24, 2023

      You have to make the first commit in your local repo, before you push to GitHub.

      • William Torrez
        William TorrezDec 25, 2023

        Why my progress is saved in the branch master?

        I want save my progress in the branch main but my progress is saved in the branch master

        README is saved in main

        Image description

        My progress is saved in the branch master

        Image description

        My structure of git

        Image description

        I get the following error:

        git push pb main

        To https://github.com/Villelmo/Beginning_Perl.git
        ! [rejected] main -> main (non-fast-forward)
        error: failed to push some refs to 'https://github.com/Villelmo/Beginning_Perl.git'
        hint: Updates were rejected because the tip of your current branch is behind
        hint: its remote counterpart. Integrate the remote changes (e.g.
        hint: 'git pull ...') before pushing again.
        hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    • Cezary Mazur
      Cezary MazurDec 24, 2023

      If you didnt clone your repository from GitLab/GitHub/Bit bucket etc your origin is not set. You need set your repo url to be able to push your files there😊

Add comment