DEV Community

Cover image for Git Basics: A Guide to Efficient Version Control and Collaborative Development
Syed Sohan
Syed Sohan

Posted on • Originally published at syedsohan.hashnode.dev

Git Basics: A Guide to Efficient Version Control and Collaborative Development

Introduction

Version control is a critical component of modern software development, and Git is one of the most popular version control systems available. It provides an efficient way of tracking changes to your codebase, collaborating with other developers, and rolling back changes if necessary. In this blog post, we will cover the basics of Git, including creating a repository, the Git workflow, essential Git commands, and undoing changes.

  • Creating a Git repository

To create a Git repository, you must first navigate to the directory where you want to create the repository. Once you are in the appropriate directory, run the git init command to initialize the repository. This command creates a hidden directory named .git in the current directory that contains all of the metadata that Git uses to track changes to your files.

Example:

cd /path/to/directory
git init
Enter fullscreen mode Exit fullscreen mode

This creates an empty Git repository in the /path/to/directory directory.

  • Git workflow: working directory, staging area, repository

Git provides several essential commands that you will use frequently:

git add: This command adds changes from your working directory to the staging area. You can specify which files you want to add, or use git add . to add all changes.

Example:

Suppose you have made changes to several files in your working directory. You can add all of these changes to the staging area using the git add . command:

git add .
Enter fullscreen mode Exit fullscreen mode

This adds all changes in your working directory to the staging area.

git commit: This command creates a new commit with the changes in the staging area. You can add a commit message using the -m flag.

Example:

Suppose you have added changes to the staging area using the git add command. You can now commit these changes to the repository with a commit message using the git commit command:

git commit -m "Add new feature"
Enter fullscreen mode Exit fullscreen mode

This creates a new commit in the repository with the message "Add new feature".

git status: This command shows you the status of your working directory and staging area.

Example:

Suppose you have made changes to several files in your working directory, and you have added some of these changes to the staging area. You can use the git status command to see the status of your working directory and staging area:

git status
Enter fullscreen mode Exit fullscreen mode

This displays information about the files that have been modified, the files that are staged for commit, and the files that are not tracked by Git.

git log: This command shows you a log of all the commits in the repository.

Example:

Suppose you want to view a log of all the commits in the repository. You can use the git log command to display a list of commits:

git log
Enter fullscreen mode Exit fullscreen mode

This displays a list of all the commits in the repository, including the commit message, author, date, and commit hash.

git diff: This command shows you the differences between the files in your working directory and the files in the staging area or repository.

Example:

Suppose you have made changes to a file in your working directory, but you haven't added these changes to the staging area. You can use the git diff command to see the differences between the working directory and the staging area:

git diff
Enter fullscreen mode Exit fullscreen mode

This displays the differences between the files in your working directory and the files in the staging area.

  • Undoing changes in Git

Git provides several ways to undo changes that you have made to your codebase:

git checkout: This command lets you discard changes in your working directory and revert to the last committed version of the file.

Example:

Suppose you have made changes to a file in your working directory, but you want to discard these changes and revert to the last committed version of the file. You can use the git checkout command to do this:

git checkout file_name
Enter fullscreen mode Exit fullscreen mode

This discards the changes in your working directory and reverts to the last committed version of the file.

git reset: This command lets you unstage changes that you have added to the staging area.

Example:

Suppose you have added changes to the staging area using the git add command, but you want to unstage these changes. You can use the git reset command to do this:

git reset file_name
Enter fullscreen mode Exit fullscreen mode

This removes the changes from the staging area.

git revert: This command lets you create a new commit that undoes the changes in a previous commit.

Example:

Suppose you have made a commit that introduced a bug, and you want to undo this commit. You can use the git revert command to create a new commit that undoes the changes in the previous commit:

git revert commit_hash
Enter fullscreen mode Exit fullscreen mode

This creates a new commit that undoes the changes in the previous commit.

Summary

In this blog post, we covered the basics of Git, a popular version control system that provides an efficient way of tracking changes to your codebase, collaborating with other developers, and rolling back changes if necessary. We started with creating a Git repository and then discussed the Git workflow, which includes the working directory, staging area, and repository. We also covered essential Git commands, including add, commit, status, log, and diff, and how they can be used to manage changes in your codebase. Finally, we explored ways to undo changes in Git using the checkout, reset, and revert commands.

Conclusion

Git is a powerful tool that provides an efficient way of tracking changes to your codebase, collaborating with other developers, and rolling back changes if necessary. In this blog post, we covered the basics of Git, including creating a repository, the Git workflow, essential Git commands, and undoing changes. By mastering these concepts, you will be well on your way to becoming a proficient Git user.

Thanks

Thanks for reading this post. Stay tuned for more.

You can find me here also.

If you like my content, please consider buying me a coffee.

Buy Me A Coffee

Top comments (6)

Collapse
 
ant_f_dev profile image
Anthony Fung

A comprehensive list!

For those learning Git, I'd recommend installing Git Extensions if you're on Windows. It offers a GUI to help with some of these commands. As a bonus, it will show you the corresponding command-line commands as it does them.

Collapse
 
syedsohan profile image
Syed Sohan

Thanks for your recommendation!

Collapse
 
ant_f_dev profile image
Anthony Fung

No Problem. The link is gitextensions.github.io/

Version 3 had support for theming/dark mode. At time of writing this, changes in version 4 meant that theming is unavailable (as far as I understand). If that is important, it's still possible to get v3 from the release history.

Collapse
 
acode123 profile image
acode123

:)

Collapse
 
jwp profile image
John Peters

I do recommend the Visual Studio Code plug-in named GitLens. A very nice tool for helping with Git commands.

Collapse
 
syedsohan profile image
Syed Sohan

Yes, it's a nice one.