DEV Community

sathish kumar
sathish kumar

Posted on

Learn the basics of git

Before we dive deep into the technical details, let's list what we are going to learn from this article.

  1. What exactly is Git
  2. Visual representation of Git
  3. Who uses git
  4. Git commit workflow
  5. Basics commands of git 

What exactly is Git

Git is the version control software that tracks code changes and manages the history of changes. Some of the examples of version control software allow users to revisit earlier versions of the file changes, undo changes, compare changes between versions, merge the changes from different users, and much more.

One of the use cases where git version control software can be used, for instance: Is in an organization where many developers make code and they need a tool to integrate/collaborate the changes without overriding other developer code changes and release the software in a stable manner or unbroken code.

Visual representation of Git

Let's say you initialize a web application project and like to create an HTML page where you are adding a header, body, and footer by making commits in git repository in different point in time (commit generally means a snapshot of the code).

visual representation of git

Assume your team lead says, let's remove the footer. And we can easily do that by going back to the previous commit in our repo.

visual representation of git

Who uses Git

  • Software developers: Git is widely used by software developers to manage their source code and collaborate with other developers on projects.
  • System administrators: Git can be used by system administrators to manage configuration files and scripts across multiple servers.
  • Designers: Git can also be used by designers to manage design assets and collaborate with other designers on projects.
  • Project managers: Git can be used by project managers to track changes to project files and collaborate with team members on project development.
  • Researchers: Git is increasingly being used by researchers to manage code, data, and analysis scripts for their research projects.
  • Writers: Git can also be used by writers to track changes to written documents and collaborate with other writers on projects.

Git Commit Workflow

Let's try to understand how this git commit workflow works by breaking down these concepts working directory, staging area, and repository.

Working directory

Is a place where we add, delete, update, and save files.

Staging area

Select a group of files that we prepare to make a commit

Commit

Make a snapshot of the file changes that were added previously in the staging area which creates a commit id and store it in the git repository. Then later we can track the changes by referring to commit history.

commit workflow

Basic commands of Git

Git Init

Initializing a Git repository: To start using Git, you need to initialize a Git repository in your project directory. You can do this by running the command in your project directory.

git init
Enter fullscreen mode Exit fullscreen mode

git init

Make changes

Once you have initialized the Git repository, you can start working on files. Add some HTML and CSS files in the project working directory index.html, navbar.html, and sample.css

Git status

Checking the status of the repository: You can check the status of the repository at any time using the below command. This will show you which files have been modified or added to the repository.

git status
Enter fullscreen mode Exit fullscreen mode

git status

Adding files to the staging area: 

You can add files using the below command to add specific files or all files in the current directory.

git add <filename> or git add .
Enter fullscreen mode Exit fullscreen mode

git add

Committing changes to git repo: 

After you have added files to the repository, you can commit the changes using the command. This creates a snapshot of the changes you made to the files.

git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

git commit

Viewing commit history: 

You can view the commit history of the repository using the below command. This will show you a list of all the commits that have been made, along with their commit messages and other details.

git log
Enter fullscreen mode Exit fullscreen mode

git log

Branching: 

Git allows you to create branches in the repository, which are essentially copies of the code base that you can work on independently. You can create a new branch using the command.

git branch <branchname>
Enter fullscreen mode Exit fullscreen mode

Merging: 

Once you have made changes on a branch and want to merge those changes back into the main branch, you can use the command.

git merge <branchname>
Enter fullscreen mode Exit fullscreen mode

These are some of the basic Git commands and concepts. Git is a powerful tool with many features, but these basics should be enough to get you started with version control.

Conclusion

These are just some of the basic Git commands and concepts that you will need to get started with version control. Git is a powerful tool with many features, and it can take some time to master all of them. However, by learning these basics, you will be well on your way to becoming a proficient Git user.

Top comments (0)