A Comprehensive Guide to Git: Mastering Version Control with Commands

August 10, 2023by Dhawal

Git, the distributed version control system, has transformed the way developers manage their code, collaborate with others, and track changes over time. This comprehensive blog post aims to provide you with a detailed understanding of Git’s core concepts and functionalities, accompanied by a comprehensive list of essential commands. Whether you’re a beginner or an experienced developer looking to expand your Git skills, this guide has you covered.

 

Getting Started with Git

 

  • Configuring Your Identity

The git config command manages Git settings and configurations for your local or global environment.

 

$ git config --global user.name "xyz"
$ git config --global user.email xyz@gmail.com

 

  • Initializing a Repository

The git init command initializes a new Git repository in the current directory.

 

$ git init

 

  • Staging Changes (git add)

The git add command stages changes, preparing them to be committed in the next step of the Git workflow.

 

$ git add <FileName>

 

  • Committing Changes (git commit)

The git commit command records staged changes in the repository, creating a new snapshot with a descriptive message.

 

$ git commit -m “Commit Message”

 

  • Checking Repository Status (git status)

The git status command displays the current state of your repository, showing modified, staged, and untracked files.

 

$ git status

 

  • Viewing Commit History (git log)

The git log command provides a chronological history of commits in the repository, showing details like author, date, and commit messages.

 

$ git log

 

  • Viewing Commit History in One Line (git log)

The git log –oneline command provides a compact one-line summary of commit history, ideal for a quick overview.

 

$ git log –oneline

 

  • Creating a New Branch (git branch)

The git branch command lists, creates, or deletes branches within your repository, aiding in code organization and parallel development.

 

$ git checkout -b <New_BranchName>

 

  • Switching Between Branches (git checkout)

The git checkout command switches between branches or commits, allowing you to navigate different versions of your project.

 

$ git checkout <BranchName>

 

  • Merging Branches (git merge)

The git merge command integrates changes from one branch into another, combining their histories and content.

 

$ git merge <BranchName>

 

  • Reset Commit

The git reset command undoes changes, moving the current branch pointer to a specified commit while preserving changes as unstaged.

Find the previous commit id to move to (7digit beginning hexa number say: 9a9add8)

 

git reset <commit-id>

 

  •  Adding Remote Repositories (git remote add)

The git remote add origin command establishes a connection between your local repository and a remote repository, typically on platforms like GitHub or GitLab.

 

$ git remote add origin <github-repository-url>

 

  • Fetching Changes from Remote (git clone)

The git clone command duplicates a remote repository onto your local machine, creating a copy for collaboration or personal use.

 

$ git clone <github-repository-url>

 

  • Pulling Changes from Remote (git pull)

The git pull command fetches and merges changes from a remote repository into your current branch, ensuring your local copy is up-to-date.

 

$ git pull '<github-repository-url> 'master'

 

  • Pushing Changes to Remote (git push)

The git push command sends your local commits to a remote repository, updating its branch with your latest changes.

 

$ git push -u origin master

 

 

 

Conclusion:

With this comprehensive guide to Git commands, you now have the tools and knowledge to navigate the version control landscape confidently. Git empowers you to work efficiently, collaborate seamlessly, and track changes meticulously, whether you’re developing solo or as part of a team. As you continue your journey with Git, remember that practice and experimentation are key to truly mastering its capabilities. Embrace version control, explore its commands, and watch your coding projects flourish like never before. Happy coding!