DEV Community

Cover image for How to use Git | Commands and their Explanations
Niraj Narkhede
Niraj Narkhede

Posted on • Edited on

How to use Git | Commands and their Explanations

This guide walk you through how to use git, covering every command you'll need to navigate your way through version control like a pro. Whether you're a newcomer or a seasoned web developer, this post will provide valuable insights to enhance your workflow.

What is Git?

To start things off, let's address the elephant in the room. What exactly is Git? Git is a distributed version control system. Unlike centralized version control systems, Git doesn't rely on a single repository to store all versions of your project. Instead, every developer has a complete copy on their own machine. This makes Git incredibly fast, scalable, and ideal for collaborating with teams of all sizes.

Before diving into the commands, let’s get Git up and running on your system.

Installation

  1. Windows:
    • Download the Git installer from Git's official website.
    • Run the installer, and follow the on-screen prompts. Feel free to use the default settings.
  2. macOS:

    • Use Homebrew to install Git. Open a terminal and enter:
      brew install git
    
  3. Linux:

    • Use your package manager. For Ubuntu/Debian, run:
      sudo apt-get install git
    

Configuration

Once installed, it's time to set up your identity. This information will be used in commit messages:

git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"
Enter fullscreen mode Exit fullscreen mode

To confirm your configurations:

git config --list
Enter fullscreen mode Exit fullscreen mode

Starting a Git Repository

Now that you have Git set up, let's start using it.

Initializing a Repository

To create a new Git repository, navigate to your project directory and run:

git init
Enter fullscreen mode Exit fullscreen mode

This command sets up a new repository, creating a .git directory in your project.

Cloning an Existing Repository

If you want to contribute to an existing project, you can clone it to your local machine:

git clone https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

This creates a copy of the specified repository on your machine.


Basic Commands

With the repository in place, let's explore some basic commands that you'll frequently use.

Checking the Status

To view the status of your working directory and staging area, run:

git status
Enter fullscreen mode Exit fullscreen mode

This command shows which files are staged, modified, or untracked.

Adding Changes

When you make changes to files, you need to add them to the staging area before committing them:

git add filename
Enter fullscreen mode Exit fullscreen mode

To add all files at once:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes

After staging your changes, you'll need to commit them:

git commit -m "Brief message describing the changes"
Enter fullscreen mode Exit fullscreen mode

This command saves your changes in the repository. The message should be concise yet descriptive.

Viewing Commit History

To see the commit history, use:

git log
Enter fullscreen mode Exit fullscreen mode

This command displays all past commits, along with their messages. For a more compact view:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Branching is one of Git's most powerful features, allowing you to work on different tasks simultaneously.

Creating a Branch

To create a new branch:

git branch new-branch
Enter fullscreen mode Exit fullscreen mode

Switching Branches

To switch to another branch:

git checkout new-branch
Enter fullscreen mode Exit fullscreen mode

Merging Branches

Once you're done working on a branch, you can merge it back into the main branch:

git checkout main
git merge new-branch
Enter fullscreen mode Exit fullscreen mode

Deleting a Branch

After merging, you may want to delete the branch:

git branch -d new-branch
Enter fullscreen mode Exit fullscreen mode

Remote Repositories

Collaborating with others requires interacting with remote repositories.

Adding a Remote

To add a remote repository:

git remote add origin https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

Viewing Remotes

To list your remote repositories:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Pushing Changes

To push your changes to a remote repository:

git push origin branch-name
Enter fullscreen mode Exit fullscreen mode

If it's your first time pushing a branch:

git push -u origin branch-name
Enter fullscreen mode Exit fullscreen mode

Pulling Changes

To update your local repository with changes from a remote repository:

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

Advanced Commands

Let's dive deeper into some advanced commands that offer more control over your repository.

Stashing Changes

If you need to switch branches but aren't ready to commit your changes, you can stash them:

git stash
Enter fullscreen mode Exit fullscreen mode

To reapply the stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Viewing Diffs

To see changes between commits or working trees:

git diff
Enter fullscreen mode Exit fullscreen mode

Resetting Commits

To undo a commit and move the changes back to the staging area:

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Reverting Commits

To create a new commit that undoes a previous commit:

git revert commit-id
Enter fullscreen mode Exit fullscreen mode

Rebasing Branches

To reapply commits on top of another base tip:

git rebase branch-name
Enter fullscreen mode Exit fullscreen mode

Solving Conflicts

When multiple changes affect the same part of a file, a conflict occurs. Git provides tools to resolve these conflicts.

Conflict Indicators

When a conflict happens, the affected file contains conflict markers:

<<<<<<< HEAD
Your changes
=======
Changes from the other branch
>>>>>>>
Enter fullscreen mode Exit fullscreen mode

You'll need to edit the file to mark the resolved content.

Adding and Committing Resolved Files

After resolving conflicts, mark the file as resolved:

git add filename
Enter fullscreen mode Exit fullscreen mode

Then, commit the resolution:

git commit
Enter fullscreen mode Exit fullscreen mode

Git Workflows

Understanding different workflows can make managing your projects easier.

Feature Branch Workflow

  • Create a Branch: Make a new branch for each feature or bug fix.
  • Work on the Branch: Commit your changes to this branch.
  • Merge: Once done, merge it back into the main branch.
  • Delete the Branch: Clean up by deleting the branch after merging.

Forking Workflow

  • Fork: Create a copy of the repository on your GitHub account.
  • Clone: Clone your forked repository.
  • Feature Branch: Follow the feature branch workflow.
  • Pull Request: Create a pull request to merge changes from your forked repo into the original repo.

GUI Tools vs. Command Line

While the command line is powerful, GUI tools provide a more visual experience. Some popular Git GUI tools include:

  • GitKraken: A feature-rich Git client with an intuitive interface.
  • SourceTree: A free Git client that offers detailed views of your repositories.
  • GitHub Desktop: Simplifies Git workflows specifically for GitHub repositories.

Conclusion

Using Git effectively can be a game-changer for web developers. From tracking changes to collaborating seamlessly, mastering these commands will significantly enhance your workflow. Remember, practice makes perfect, and with time, navigating Git will become second nature. Happy coding!

By incorporating Git into your development process, you'll not only keep your work organized but also be prepared to tackle any challenges that come your way. How has Git changed your workflow? Share your experiences and any tips you might have in the comments below!

Top comments (9)

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

well this is for beginners for advance level you must know about

  • git log -d tag-name
  • git log --no-merges
  • git log --author =="Syed-Ali"
  • git log --grep = "bug message "
  • git log --stat
  • git log p -1
Collapse
 
nnnirajn profile image
Niraj Narkhede
Collapse
 
chaopas profile image
ChaoPas

Good

Collapse
 
chaopas profile image
ChaoPas

git log p -1 expalin

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done 👏

Here is also a free ebook in case that anyone wants to learn more:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
 
0x7461696c73 profile image
Enzo Broussard

Very helpful thank you.

Collapse
 
nnnirajn profile image
Niraj Narkhede
Collapse
 
codeveronica profile image
Luma

Thank you, this is great!

Collapse
 
nnnirajn profile image
Niraj Narkhede