DEV Community

Cover image for Git Cheatsheet that will make you a master in Git
Mirza Saikat Ahmmed
Mirza Saikat Ahmmed

Posted on

Git Cheatsheet that will make you a master in Git

Introduction to Git

Git is a widely used version control system that allows developers to track changes and collaborate on projects. It has become an essential tool for managing code changes, whether working solo or in a team. However, mastering Git can be a challenge, especially for beginners who are not familiar with its commands and features. In this Git cheatsheet, we will cover both the basic and advanced Git commands that every developer should know. From creating a repository to branching, merging, and beyond, this cheatsheet will serve as a handy reference guide for anyone looking to improve their Git skills and become a more proficient developer. Whether you are just starting with Git or looking to enhance your existing knowledge, this cheatsheet will help you make the most out of Git and optimize your workflow.

Basic Git commands

Initialization

To initialize a new Git repository in the current directory, run the following command:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a hidden .git directory in the current directory that tracks changes to your code.

Cloning

To clone an existing Git repository to your local machine, run the following command:

git clone <repository URL>
Enter fullscreen mode Exit fullscreen mode

This creates a new directory on your computer with a copy of the repository.

Staging changes

Before you commit changes to your code, you need to stage them using the git add command. This tells Git which changes you want to include in your commit.
To stage a file or directory, run the following command:

git add <file/directory>
Enter fullscreen mode Exit fullscreen mode

You can also stage all changes in the current directory by running:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing changes

To commit the changes in the staging area with a commit message, run the following command:

git commit -m "<commit message>"
Enter fullscreen mode Exit fullscreen mode

The commit message should briefly describe the changes you made in the commit.

Checking status

To check the current status of your repository, run the following command:

git status
Enter fullscreen mode Exit fullscreen mode

This will show you which files have been modified, which files are staged for commit, and which files are untracked.

Viewing changes

To view the changes between the working directory and the staging area, run the following command:

git diff
Enter fullscreen mode Exit fullscreen mode

To view the changes between the staging area and the last commit, run the following command:

git diff --cached
Enter fullscreen mode Exit fullscreen mode

Branching

Git allows you to create multiple branches of your code so that you can work on different features or fixes without affecting the main codebase. The default branch in Git is called master.

To create a new branch with the specified name, run the following command:

git branch <branch name>
Enter fullscreen mode Exit fullscreen mode

To switch to the specified branch, run the following command:

git checkout <branch name>
Enter fullscreen mode Exit fullscreen mode

You can also create and switch to a new branch in one command by running:

git checkout -b <branch name>
Enter fullscreen mode Exit fullscreen mode

To merge the specified branch into the current branch, run the following command:

git merge <branch name>
Enter fullscreen mode Exit fullscreen mode

Pushing changes

To push changes to a remote repository, run the following command:

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

is the name of the remote repository, and is the name of the branch you want to push.

Pulling changes

To pull changes from a remote repository, run the following command:

git pull <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

<remote> is the name of the remote repository, and <branch> is the name of the branch you want to pull.

Viewing history

To view the commit history, run the following command:

git log
Enter fullscreen mode Exit fullscreen mode

This will show you a list of all the commits in the repository, along with the commit message, author, and date.

Advanced Git commands

Reverting changes

If you need to undo a commit, you can use the git revert command. This creates a new commit that undoes the changes made in the specified commit.

Resetting changes

If you want to undo a commit and remove it from the commit history, you can use the git reset command. This removes the specified commit and all subsequent commits from the commit history. There are three options for git reset: --soft, --mixed, and --hard.

--soft
only resets the commit history and leaves the changes in the staging area.
--mixed resets the commit history and unstages the changes.
--hard resets the commit history, unstages the changes, and discards all changes made after the specified commit.

To reset the last commit using --soft, run the following command:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

To reset the last commit using --mixed, run the following command:

git reset --mixed HEAD~1
Enter fullscreen mode Exit fullscreen mode

To reset the last commit using --hard, run the following command:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Rebasing

If you want to apply your changes to a different branch, you can use the git rebase command. This command applies your changes on top of the specified branch.
To rebase the current branch onto the specified branch, run the following command:

git rebase <branch name>
Enter fullscreen mode Exit fullscreen mode

Stashing

If you want to save changes without committing them, you can use the git stash command. This saves the changes in a stack of temporary commits, allowing you to switch to a different branch or work on something else.
To stash your changes, run the following command:

git stash
Enter fullscreen mode Exit fullscreen mode

To apply your changes again, run the following command:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Cherry-picking

If you want to apply a specific commit from one branch to another, you can use the git cherry-pick command. This command applies the specified commit on top of the current branch.
To cherry-pick the specified commit, run the following command:

git cherry-pick <commit hash>
Enter fullscreen mode Exit fullscreen mode

Git hooks

Git hooks are scripts that run automatically before or after specific Git commands, allowing you to customize the behavior of Git. Git comes with a set of built-in hooks, but you can also create your own custom hooks.
To create a custom Git hook, navigate to the .git/hooks directory in your Git repository and create a new file with the name of the hook you want to create (e.g., pre-commit, post-merge). The file should be executable and contain the script you want to run.

Git aliases

Git aliases are shortcuts for Git commands, allowing you to save time and type less. You can create your own custom aliases using the git config command.
To create a new alias, run the following command:

git config --global alias.<alias name> '<command>'
Enter fullscreen mode Exit fullscreen mode

<alias name> is the name of the alias you want to create, and <command> is the Git command you want to alias.

Git workflows

Git workflows are strategies for using Git to manage code changes in a team. There are several popular Git workflows, including the centralized workflow, the feature branch workflow, and the Gitflow workflow.
The centralized workflow is a simple workflow that involves a single main branch, with all changes made directly to that branch.
The feature branch workflow involves creating a new branch for each feature or bug fix, and merging those branches back into the main branch when the changes are complete.
The Gitflow workflow is a more complex workflow that involves multiple branches, including a develop branch for ongoing development, a release branch for preparing releases, and feature branches for individual features.

Conclusion

In conclusion, Git is a powerful tool for version control and managing code changes. It allows developers to collaborate on projects, track changes, and revert to previous versions when necessary. While the basic Git commands are essential to know, the advanced Git commands discussed in this cheat sheet can help you be more efficient and effective when working with Git.

You can connect with me on Linkedin: https://www.linkedin.com/in/mirzasaikatahmmed/
Follow me on GitHub: https://github.com/mirzasaikatahmmed

Top comments (1)

Collapse
 
leonardo_porto_3d6599b8d4 profile image
leonardo porto

Very useful!!