DEV Community

Cover image for Mastering Git: Concepts and code examples
Daniel for BLST

Posted on

Mastering Git: Concepts and code examples

Git is a powerful version control system that has revolutionized the way developers collaborate and manage their code. However, mastering Git can be a challenge for beginners and even experienced developers. In this article, we'll explore the concepts of Git and provide real-life examples to help you understand how to use Git effectively.

Git Concepts

Before we dive into Git commands, let's review some of the key concepts that are essential to understanding how Git works:

  • Repository: A Git repository is a folder or directory that contains your code and all the files necessary to run it.
  • Commit: A commit is a snapshot of your code at a specific point in time. It includes all the changes you've made since your last commit.
  • Branch: A branch is a separate version of your code that allows you to work on new features or fixes without affecting the main codebase.
  • Merge: Merging is the process of combining two or more branches into a single branch. It's a common practice for collaborating on a project.
  • Pull Request: A pull request is a request to merge changes from one branch to another. It's often used for code reviews and collaboration.

Now that we've covered some of the basic Git concepts, let's explore some real-life examples to see how they apply in practice.

Creating a new branch

To create a new branch, use the following command:

git checkout -b new-branch

Enter fullscreen mode Exit fullscreen mode

This will create a new branch called new-branch and switch to it.

Making a commit

To make a commit, use the following command:

git commit -m "Your commit message here"

Enter fullscreen mode Exit fullscreen mode

This will create a new commit with your changes and add a commit message to describe what you've done.

Merging a branch

To merge a branch into another branch, use the following command:

git checkout main
git merge new-branch
Enter fullscreen mode Exit fullscreen mode

This will switch to the main branch and merge the changes from new-branch into it.

Rebase

Rebasing is the process of moving the base of a branch to a different commit. It's often used to keep your Git history clean and organized. To rebase a branch, use the following command:

git checkout feature-branch
git rebase main
Enter fullscreen mode Exit fullscreen mode

This will move the base of the feature-branch to the latest commit on main.

Cherry-pick

Cherry-picking is the process of selecting a specific commit and applying it to another branch. It's often used to backport fixes or apply specific changes to different branches. To cherry-pick a commit, use the following command:

git cherry-pick <commit-hash>

Enter fullscreen mode Exit fullscreen mode

This will apply the changes from the specified commit to your current branch.

Interactive Rebase

Interactive rebase is a powerful tool that allows you to edit, delete, or reorder commits in your Git history. It's often used to clean up your Git history before merging a branch. To perform an interactive rebase, use the following command:

git rebase -i <commit-hash>

Enter fullscreen mode Exit fullscreen mode

This will open a text editor with a list of commits that you can edit, delete, or reorder.

Submodules

Submodules allow you to include one Git repository inside another. It's often used to manage dependencies or include shared code in multiple projects. To add a submodule, use the following command:

git submodule add <repository-url> <path>

Enter fullscreen mode Exit fullscreen mode

This will add the specified repository as a submodule in your current repository.

Stashing

Stashing is the process of saving your changes temporarily and reverting to the previous commit. It's often used to switch branches or update your codebase without committing unfinished work. To stash your changes, use the following command:

git stash save "Your stash message here"

Enter fullscreen mode Exit fullscreen mode

This will save your changes and revert to the previous commit. To apply your changes later, use the following command:

git stash apply

Enter fullscreen mode Exit fullscreen mode

This will apply your changes from the last stash.

Git Reset
The git reset command is used to undo changes to your Git history. This command can be used to reset your current branch to a previous commit, unstage changes, or move commits between branches.

Here are the different options for the git reset command:

git reset --soft <commit>
Enter fullscreen mode Exit fullscreen mode

This option resets your branch to the specified commit, but keeps your changes in the staging area. Use this option if you want to uncommit your changes, but keep them available to be committed again later.

git reset --mixed <commit>
Enter fullscreen mode Exit fullscreen mode

This (default) option resets your branch to the specified commit and clears the staging area. Use this option if you want to unstage your changes and keep them in your working directory.

git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

This option resets your branch to the specified commit and discards all your changes. Use this option if you want to completely undo your changes and start fresh.

Here are some precautions you should take when using the git reset --hard command:

  • Double-check the commit hash: Make sure you're specifying the correct commit hash when using git reset --hard, as this command cannot be undone.
  • Make a backup: Before using git reset --hard, make a backup of your codebase to a separate location, such as a different folder or a remote repository.
  • Use Git reflog: Git reflog is a powerful tool that allows you to view the history of your Git commands, even if they've been undone. This can be helpful if you accidentally use git reset --hard and need to recover your lost changes.

The Dangerous Git Push -f

The git push force command is used to force Git to overwrite the remote repository with your local changes, even if they conflict with changes made by others.

When to use git push force:

Honestly I would never use this command. I fear it too much. But..
You should use the git push force command with caution and only when you're absolutely sure that you want to overwrite the remote repository with your local changes. This command should only be used in cases where you need to undo changes made by others or when you're the only person working on the codebase.

Precautions to take when using git push force:

Before using git push force, communicate with your team to make sure that you're not overwriting changes made by others.
Create a backup: Before using git push force, create a backup of the remote repository to a separate location, such as a different folder or a remote repository.
Use Git reflog: Git reflog is a powerful tool that allows you to view the history of your Git commands, even if they've been undone. This can be helpful if you accidentally use git push force and need to recover lost changes.

To use git push force, use the following command:

git push --force

Enter fullscreen mode Exit fullscreen mode

Git reflog

Git reflog is a command that allows you to view the history of your Git commands, even if they've been undone. This can be helpful if you accidentally use a command like git reset --hard or git push --force and need to recover lost changes.

To use Git reflog, simply type git reflog into the terminal. This will show you a list of all the recent Git commands you've used, along with the commit hash and a brief description.

Git Init

The git init command is used to create a new Git repository. When you use this command, Git will create a new directory with a .git subdirectory that contains all the files necessary for version control.

To create a new Git repository, navigate to the directory where you want to store your code and run the following command:

git init

Enter fullscreen mode Exit fullscreen mode

This will create a new Git repository in the current directory.

Git Add

The git add command is used to add changes to the staging area. When you make changes to your code, they are initially only stored in your working directory. To commit these changes, you must first add them to the staging area using the git add command.

To add changes to the staging area, use the following command:

git add <file>

Enter fullscreen mode Exit fullscreen mode

This will add the specified file to the staging area. If you want to add all changes in the current directory, you can use the following command:

git add .
Enter fullscreen mode Exit fullscreen mode

This will add all changes in the current directory to the staging area.

Git Clone

The git clone command is used to create a copy of an existing Git repository. When you use this command, Git will copy all the files from the remote repository to your local machine.

To clone a Git repository, use the following command:

git clone <repository URL>

Enter fullscreen mode Exit fullscreen mode

This will create a copy of the remote repository in a new directory on your local machine.

Git Pull

The git pull command is used to update your local repository with the latest changes from the remote repository. When you use this command, Git will fetch the latest changes from the remote repository and merge them into your local branch.

To update your local repository with the latest changes, use the following command:

git pull

Enter fullscreen mode Exit fullscreen mode

This will fetch the latest changes from the remote repository and merge them into your local branch.

Image description

Thank you for reading up to this point! I hope you found something here helpful :D

Star our Github repo and join the discussion in our Discord channel!
Test your API for free now at BLST!

Top comments (0)