Mastering Git Commands

Mastering Git Commands

Essential Commands for Every Developer

ยท

8 min read

In the fast-paced world of software development, version control is the backbone of collaborative coding. It keeps chaos at bay, enables seamless teamwork, and provides the safety net needed to explore and experiment with code. At the heart of version control systems, Git remains supreme.

But here's the catch: Git, with its incredible power and flexibility, can be a double-edged sword. Without a solid understanding of its basic commands, it's easy to feel lost in a sea of code changes, branches, and repositories. That's why, whether you're a seasoned developer or just starting your coding journey, mastering Git's essential commands is a non-negotiable skill.

In this blog, we're embarking on a journey through the fundamental Git commands that every developer, regardless of experience level, should have in their toolkit. Whether you're collaborating on a large-scale project, managing your codebase, or simply striving for greater coding efficiency, these commands will be your trusted companions.

Get ready to level up your version control game as we explore the basic Git commands that can make a world of difference in your development journey. Let's dive in! ๐Ÿ’ป๐Ÿš€

Basic Git concepts

Before learning the basic git commands, we have to know some of the basic concepts of git to get started. Below is the list of basic git concepts which is required in day to day life of a programmer.

  • Repository (Repo): A repository is a directory where Git stores all the files and history for a project. It can be local (on your computer) or remote (hosted on a server, like GitHub )

  • Commit: A commit represents a snapshot of the project at a specific point in time. It includes changes to one or more files, along with a unique identifier (SHA-1 hash), a commit message explaining the changes, and a reference to the previous commit (parent commit) to create a history.

  • Branch: A branch is a parallel line of development within a Git repository. It allows multiple contributors to work on different features or bug fixes simultaneously without interfering with each other. The default branch is usually called master or main. In the recent version of GitHub the default branch is always main, so it is good to make your branch main in your local repository.

    ๐Ÿ’ก
    A particular branch is related to one pull request. So if you want to make another pull request for another feature then make another branch for that feature.
  • Checkout: Checking out a branch means switching from one branch to another.

  • Remote: A remote is a repository hosted on a different server. You can say that a remote repository is a repository that is not located on your local computer and is stored in GitHub. Developers can clone a remote repository to their local machine, collaborate on it, and push changes back to the remote.

  • Clone: Cloning is the process of copying a remote repository to your local machine.

    It creates a local repository with the same commit history and files as the remote.

  • Pull: Pulling is used to update your local repository with changes from a remote repository. It combines a fetch operation (retrieving changes from the remote) and a merge operation (combining those changes with your local branch).

  • Push: Pushing is used to send your local commits to a remote repository.

    It updates the remote repository with your changes.

  • Merge: Merging combines two or more branches (usually the current branch and another) into a single branch. It's used to integrate changes from one branch into another.

  • Conflict: Conflicts occur when Git cannot automatically merge changes from two different branches or commits. Developers must manually resolve conflicts by choosing which changes to keep.

  • Pull Request (PR): In some Git hosting platforms like GitHub, a pull request is a way to propose changes to a repository. Others can review the changes, discuss them, and then merge them into the main branch.

  • Staging Area (Index): The staging area is where you prepare changes for a commit. You can selectively add specific changes to the staging area before committing them.

  • .gitignore: This is a file that specifies which files or directories should be ignored by Git (not tracked). It's useful for excluding temporary files, build artifacts and sensitive information from version control.

Git Basic Commands

Now let's talk about the basic git commands and their use cases.

  1.   git init
    

    The above command is used to initialize any local folder to a git repository i.e. from now you can see the log history of this folder and the changes that are going to be made in this particular folder.

  2.   git clone <Repository-URL>
    

    The above command is used to clone or download any repository files from GitHub by using the repository URL. In the above command, you have to replace the <Repository-URL> to the URL of the repository which you want to clone.

  3.   git status
    

    The above command is used to list the files in the present git folder. It will list the untracked files (i.e. newly created files), modified files, deleted files and more. It will only show the changes from the time when the folder is initialized as a git folder by git init command.

  4.   git add <File-Name> 
      # It will add the specified file for commit
      git add .
      # It will add all the untracked and modified files for commit
    

    The above commands are used to add the files which will be committed soon. That means we are just selecting the files for a commit.

    The first command is used to add a specific file by the <File-Name> and the second command is used to add all the untracked and modified files (which are shown in the git status command ) for commit.

  5.   git commit -m "Message for this commit"
    
      # or
    
      git commit -S -m "Message for this signed commit"
    

    The above command is used to commit the added files with a message. The -m flag is used to give a commit message. The -S flag is used to sign a commit.

    ๐Ÿ’ก
    If you didn't read my previous blog on Git Security, where I explained all the concepts of signed commits and how to set security keys to sign a commit, then go to the below link and read the full blog: https://codago.hashnode.dev/enhancing-git-security
  6.   git log
    

    The above command is used to display all the previous commits of that particular repository with various important information like commit message, commit ID, and the time when the commit was made. So, it is a very useful command.

  7.   git stash
    

    The above command is used to put all the added files in a stash area. A stash area is a place where you prepare changes for a commit and can get those stashed files when required.

  8.   git stash pop
    

    The above command is used to take out the files added to a stash area.

  9.   git reset <Commit-ID>
    

    The above command is used to reset all the commits above the given commit ID. You can find the commit ID of a commit by using the git log command on your console. This command will undo the commits above the given commit ID.

  10. git restore --staged <DeletedFileName>
    

    The above command is used to get back a deleted file or restore the state of a file to the previous state.

  11. git remote add origin <Repository-URL>
    

    The above command is used to add a remote URL named origin. This command is very useful because without this command you can't connect your project repository URL to your git client. The repository URL can be found in the GitHub repository of the project.

  12. git push origin <branch-name>
    
    #or
    
    git push origin <branch-name> -f
    

    The above commands are used to push the commits to the remote URL (origin) from your git client. This command will push the new commits to the GitHub repository of the project. The -f flag is used to forcefully push the commit history to the remote URL (origin).

    ๐Ÿ’ก
    It is not recommended to use the -f flag because it can break your commit history. But sometimes it may be necessary to forcefully do certain things.
  13. git branch <new-branch-name>
    

    The above command is used to make a new branch whose name is given in <new-branch-name>.

  14. git checkout <branch-name>
    

    The above command is used to checkout from the current branch to the specified branch.

  15. git pull origin <branch-name>
    

    The above command is used to update your local repository with changes from a remote repository. In this case, we are pulling from the origin remote repository and from the specified branch.


So this is it for this blog. If you find the blog useful then share it with your friends so that they also can benefit from this blog. And if you have any suggestions for a blog topic then feel free to comment down below.

And if you didn't see my previous blogs on Git then just check those out. Happy Coding โœŒ

previous blogs:

Git and GitHub Introduction: https://codago.hashnode.dev/git-and-github-introduction

Enhancing Git Security: https://codago.hashnode.dev/enhancing-git-security

ย