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
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 .
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"
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
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
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
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
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
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
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.
Top comments (6)
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.
Thanks for your recommendation!
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.
:)
I do recommend the Visual Studio Code plug-in named GitLens. A very nice tool for helping with Git commands.
Yes, it's a nice one.