Git Tutorial for Beginners: 12 Essential Git Commands Explained

What Is Git and Why Use It?

git commands

Git is the most widely used distributed version control in the industry. While working on a project collaboration is the key git can easily create and maintain multiple branches allowing different people to work on the same project.

Git is easily available in an open-source environment(free) or a dedicated Git instance for your organization(paid). whatever the case it follows the same strategies and workflows while working.

Git provides flexibility, efficiency, and reliability while working on a project and is one less thing to worry about.

In this post, we will discuss common git commands for beginners for the development and merging of the code which eventually helps in building and deploying the software applications

Basic Git Commands Every Developer Should Know

1. Setting Up a New Git Repository

Git Init

Git Init is an acronym for git initialize. It can be used within an empty directory or a project with other version control. Usually, Git Init is the first command that will be used to set the git ecosystem and for the availability of different git commands.

Once git init is executed a .git subdirectory is added to the current directory.

2. Cloning an Existing Repository

Git Clone

Git Clone copies or clones of a given repository. Git clone is the most commonly used git commands, it creates the working copy from a remote repository or a local filesystem.
It also creates a remote connection often known as ‘origin‘ pointing to the original repository.

3. Configuring Git Settings

Git Config

The Git Config command is used to read and write different configurations.

There are three different levels at which configurations can be read and written.

  1. Local – By default git config will write and read to local configuration if no config level is specified. All local config values are stored inside the repository directory /.git/config.
  2. Global – Global Configuration is a user-level configuration. it will be applied to the user’s home directory C://Users//.gitconfig
  3. System – System configuration will be applied at the machine level for all users. Config data will be stored at C:\Programdata\Git\config

Saving and Managing Changes

4. Committing Code Changes

Git Commit

Git commit captures the changes from your current repository to your ‘origin‘ repository(remote or local). It will assign alphanumeric codes for your changes before you push changes to ‘origin’.
The git commit command is used with git add and git push.

5. Comparing Code Differences

Git Diff

Git Diff will output the changes between git sources as files, commits, and branches.

6. Temporarily Stashing Changes

Git Stash

The git stash command will temporarily shelve the changes from your working directory. These changes can be applied and popped to the current branch or any other branch.

Inspecting and Reviewing Code

7. Tagging Releases

Git Tag

Git Tags are the reference points in the Git history. tags are used to capture a release of a particular version.
Tags can help create different release versions of a repository and track what changes are deployed on a particular environment or machine.

8. Identifying Code Changes

Git Blame

The Git Blame command checks the author’s metadata to specific committed lines in a file. It is mostly used while debugging the code.

Undoing and Reverting Changes

9. Reverting Commits

Git Revert

The Git Revert command will take the commit, inverse the changes from that commit, and create a new revert commit. This new revert commit will be the head of the branch. Git revert will not change the project history and it can be applied to any commit in the history.

10. Resetting the Repository

Git Reset

The Git reset Command will reset the head and current branch pointers to the specified commit. It will remove the commits done after a given commit and will update the git history which will not be done in for revert.

The git reset command will not used often in enterprise setup as it can change the git history by moving head/branch pointers. It also needs elevated privileges to perform these kinds of operations which will not be the case.

It also has three arguments for the scope of changes as –soft, –mixed,
–hard
.

11. Removing Tracked Files

Git rm

The Git rm command removes tracked files from the git Index.

Rewriting Git History

12. Rebasing Branches

Git Rebase

The Git rebase commands move or combine a sequence of commits into a new commit. It is the most used git commands while merging changes to the repository

gitrebase

In the above example, you have started working on a feature branch by creating it from the main branch. But as the feature branch progressed main branch also progressed with certain commits. Once your changes are done you want to merge them back to the main branch but without rebasing your branch with the new commits in the main it will give you merge errors. Hence it would help if you rebased so that it would look like you have all the changes from the main branch and then you can merge your changes via a merge commit.

Conclusion

Mastering Git Commands is of Utmost importance in you developer journey. It helps you to collaborate, manage code versions and effective use of git as version control system.


Comments

One response to “Git Tutorial for Beginners: 12 Essential Git Commands Explained”

  1. […] Deployment Tools Git Commands […]

Leave a Reply

Your email address will not be published. Required fields are marked *