Mastering in Basics: Essential Git commands for Beginners

Introduction

What is Git?

Git is a widely used version control system that allows multiple people to work on a project without overwriting each other’s changes. It keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

Importance of Git in DevOps

In the realm of DevOps, Git plays an indispensable role by enhancing collaboration and increasing the speed of development. It facilitates continuous integration and deployment by maintaining different versions of the code and managing the changes made by different team members.

Getting Started with Git

Installing Git

The first step in using Git is installation. Git is a command-line tool, and you can install it based on your operating system:

For Ubuntu/Linux, use the command:

sudo apt-get install git

For macOS, use Homebrew:

brew install git

For Windows, download the installer from the Git website and run it.

Setting up Git

Once Git is installed, you should configure your personal information:

git config --global user.name "Your Name" git config --global user.email "[email protected]"

These settings are important because every Git commit uses this information.

Understanding Git Basics

Git’s primary functions are carried out using commands. Here are some essential ones:

Creating a new repository

A repository (or “repo”) is a storage space for your project. It holds all the files and the history of changes made to them. To create a new repository, use the command git init:

mkdir my_project cd my_project git init

Cloning an existing repository

To work on an existing project, you can clone its repository with the command git clone followed by the URL of the repo:

git clone https://github.com/username/repository.git

Git commit

When you’ve made changes to your project that you want to save, you need to commit those changes. This involves two steps: git add and git commit. The add command stages changes for commit, and the commit command saves them:

git add . git commit -m "My first commit"

Git push

After committing your changes, you can push them to the remote repository with the git push command:

git push origin master

Git pull

To update your local repository with the latest changes from the remote, use the git pull command:

git pull origin master

Git status

To see what changes have been staged but not yet committed, you can use the git status command:

git status

Git branch

A branch is essentially a unique set of code changes with a unique name. The git branch command lets you create, list, or delete branches:

git branch my_branch

Git checkout

The git checkout command lets you navigate between the branches created by git branch:

git checkout my_branch

Git merge

The git merge command lets you take the changes from one branch (source) and apply them into another branch (destination):

git checkout master git merge my_branch

Branching and Merging in Git

In Git, branches are a part of everyday development. Here’s how you can use branches and merge them back into your main codebase.

Understanding branches

Branches are essentially pointers to a specific commit. They are useful for developing features isolated from each other. The default branch in Git is the master branch.

Creating a new branch

To create a new branch, you can use the git branch command followed by the name of the new branch.

git branch new_feature

Merging branches

When you have finished working on a feature on a dedicated branch, you can merge your changes back to the master branch, which is a common hub for all completed features.

git checkout master git merge new_feature

Resolving merge conflicts

Sometimes when you merge branches, Git won’t be able to sort out the changes—like if two branches have changed the same part of the same file. You’ll need to tell Git which code to keep. The conflicts will be marked in your code:

<<<<<<< HEAD change on the master branch ======= change on the feature branch >>>>>>> new_feature

Remove the conflict markers and keep the changes you want, then commit the result.

Exploring Advanced Git Commands

Beyond the basics, Git offers many more commands:

Git stash

If you’re in the middle of some changes but need to switch branches without committing your current changes, git stash is a great way to save changes and apply them later:

git stash

To apply the stashed changes later, use git stash apply.

Git rebase

git rebase is another way to integrate changes from one branch into another. It’s an alternative to git merge but instead of creating a new commit, it will move the feature branch changes onto the tip of master:

git checkout feature git rebase master

Git revert

To undo changes from a specific commit, use git revert. This command will create a new commit that undoes the changes made in a previous commit:

git revert <commit-hash>

Git reset

To undo changes in the staging area or your working directory, use git reset. Be careful, as this command can permanently delete uncommitted changes:

git reset --hard

Common Git Workflows

There are a few common workflows that teams using Git can follow:

  1. Centralized workflow: Similar to SVN, all changes are committed into the master branch.
  2. Feature branch workflow: Every new feature is developed on a dedicated branch and merged back into the master branch when it’s ready.
  3. Gitflow workflow: A strict branching model designed around project releases. It assigns very specific roles to different branches and defines how and when they should interact.

Best Practices for Using Git

  • Commit early and often: Git is designed to add small commits that can be understood and reviewed easily.
  • Write useful commit messages: This helps other developers understand why changes were made.
  • Use branches: They help isolate changes for features, experiments, or bugs.
  • Avoid altering published history: The version history is very important for understanding the why of your project.

Frequently Asked Questions (FAQs)

What is Git?

Git is a distributed version control system that allows developers to track changes in their code. It facilitates collaboration by enabling multiple developers to work on the same codebase without overwriting each other’s changes.

Git is an essential tool in DevOps because it enhances collaboration, tracks changes, and facilitates continuous integration and deployment. It helps manage different versions of the code and the changes made by various team members.

You can install Git based on your operating system. For Linux/Ubuntu, use the command sudo apt-get install git, for macOS, use brew install git, and for Windows, download and run the installer from the Git website.

The git commit command is used to save your changes to the local repository. Each commit is a “snapshot” of your work that you can return to or compare with others.

Both git merge and git rebase are used to integrate changes from one branch into another. The primary difference is that merge creates a new commit that merges the branches, while rebase moves the changes of one branch to the tip of the other, providing a linear commit history.

Merge conflicts occur when Git cannot automatically reconcile changes from different commits. When a merge conflict happens, Git will output a message indicating that a conflict has occurred, and the conflicted areas will be marked in your file. You need to manually edit the file to fix these conflicts and then commit the resolved changes.

A Git workflow is a recommendation about how to use Git to accomplish work in a consistent and productive manner. There are several established workflows, such as the Centralized Workflow, Feature Branch Workflow, and Gitflow Workflow.

Some best practices include making regular commits, writing clear and informative commit messages, making use of branches for new features or bug fixes, and avoiding altering published history.

Conclusion

Git is a powerful tool and an essential part of many developers’ toolkits. Mastering Git can enhance collaboration, improve workflow, and ultimately, make you a better developer. Remember, practice makes perfect. The more you use Git, the more comfortable and skilled you’ll become.

References

  1. Chacon, S., and Straub, B. (2014). “Pro Git” (2nd ed.). Apress. Available online at: https://git-scm.com/book/en/v2
  2. “Git Basics.” Git Documentation. Available online at: https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository
  3. “Git Branching.” Git Documentation. Available online at: https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
  4. “Resolving a merge conflict using the command line.” GitHub Docs. Available online at: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line
  5. Atlassian Git Tutorials. Available online at: https://www.atlassian.com/git/tutorials

Related Articles