DEV Community

Cover image for Git Cheatsheet: A Quick Reference Guide for Git Commands ✨
Dario Cruz
Dario Cruz

Posted on

Git Cheatsheet: A Quick Reference Guide for Git Commands ✨

Git is a powerful and widely used version control system for tracking changes in software development projects. Git provides a command-line interface that can be used to manage code repositories, collaborate with other developers, and track changes to code over time.

As with any command-line tool, Git can be a bit overwhelming to use at first. This article compiles dozens of instructions and tips for interacting with Git. Some of them can be helpful for both beginner and experienced developers.

Basic Git Commands 👶

  • git init: Initialize a new Git repository in the current directory.
  • git clone [url]: Clone a remote Git repository to your local machine.
  • git add [file]: Add a file to the staging area, ready to be committed.
  • git commit -m "message": Commit the staged changes with a message explaining the changes made.
  • git status: Show the status of the repository, including any changes made since the last commit.
  • git log: Show a log of all the commits made in the repository.
  • git push: Push committed changes to a remote repository.
  • git pull: Pull changes from a remote repository into your local repository.

Branching and Merging Commands 🔃

  • git branch: Show a list of all the branches in the repository.
  • git branch [branch-name]: Create a new branch with the given name.
  • git checkout [branch-name]: Switch to the given branch.
  • git merge [branch-name]: Merge changes from the given branch into the current branch.

Advanced Git Commands 😎

  • git stash: Save changes to a stack, allowing you to switch branches without committing changes.
  • git rebase: Change the order of commits, modify commit messages, and more.
  • git cherry-pick: Pick and apply specific commits from one branch to another.
  • git reset: Undo changes made to the repository.

Rebase vs Merge 🙉

In Git, there are two primary ways to integrate changes from one branch into another: rebase and merge. Both approaches have their strengths and weaknesses, and choosing the right one depends on the specific circumstances of your project.

Image description

Rebase:

Rebasing involves moving the changes from one branch onto another branch by replaying each commit on the new branch. This essentially rewrites the history of the original branch so that it appears to have been based on the new branch from the beginning. Some advantages of rebasing include:

  • A cleaner, more linear history that is easier to follow and understand.
  • Fewer merge conflicts, since the changes are integrated one at a time rather than all at once.
  • A more thorough and fine-grained review of the changes, since each commit can be examined individually.

However, there are also some disadvantages to consider when using rebasing:

  • Potentially more complex and error-prone, since it involves rewriting the history of a branch.
  • Requires force pushing to the remote repository, which can cause issues if other team members have already based their work on the original branch.

Merge:

Merging involves creating a new commit that combines the changes from one branch into another. This creates a merge commit that has two or more parent commits, one for each branch that was merged. Some advantages of merging include:

  • Simpler and less error-prone than rebasing, since it does not involve rewriting the history of a branch.
  • Can be used to merge changes from multiple branches into a single branch.

However, there are also some disadvantages to consider when using merging:

  • The commit history can become more complex and difficult to follow, especially if there are many branches and merges.
  • Merge conflicts can be more common, especially if there are many changes in both branches.

In general, rebasing is a good option for smaller, simpler changes that can be integrated quickly and with minimal conflicts. Merge is a good option for larger, more complex changes that may have conflicts and require a more flexible approach to integrating changes. Choose the best option for your particular situation.


Popular Git User Interfaces

There are also some graphical user interface (GUI) options for managing repositories. Choosing a Git UI is a matter of personal preference and project requirements. Each UI offers unique advantages and disadvantages that should be considered when selecting the best option for your project.

Ultimately, the most important factor is finding a UI that enables you to work efficiently and effectively, while meeting your team's collaboration and management needs. In this section I will be covering the pros and cons of some of the most popular Git UIs.

Image description

GitKraken

Advantages:

  • Interactive visualizations of branching and merging, which make it easy to understand the history of a repository.
  • Integration with popular project management tools like Trello and Jira, which enables seamless collaboration between team members.
  • Powerful search capabilities, including search by commit message, author, and file name.

Disadvantages:

  • Can be resource-intensive and slow on older or less powerful machines.
  • Paid version required for some advanced features.

SourceTree

Advantages:

  • Easy repository management through drag-and-drop interface, allowing you to add or remove repositories with ease.
  • Interactive visualization of branching and merging, which makes it easy to see the history of a repository.
  • Integration with popular project management tools like Jira and Trello.

Disadvantages:

  • Can be buggy or unreliable on some systems.
  • Limited customization options compared to other Git UIs.

GitHub Desktop

Advantages:

  • Integration with GitHub, making it easy to clone, create, and manage repositories on the platform.
  • Simple and user-friendly interface, with intuitive visualizations of the repository's history and changes.
  • Free and open-source.

Disadvantages:

  • Limited functionality compared to other Git UIs.
  • No integration with non-GitHub repositories.

Git Cola

Advantages:

  • Lightweight and fast, making it a good option for older or less powerful machines.
  • Customizable interface and keyboard shortcuts.
  • Support for multiple repositories and complex branching strategies.

Disadvantages:

  • Less intuitive than other Git UIs, with a steeper learning curve.
  • Limited integration with project management tools.

Conclusion 🔚

This Git cheatsheet provides a quick reference guide for some of the most commonly used Git commands. With this cheatsheet, you'll be able to get started with Git, collaborate with other developers, and track changes to code over time. However, Git is a powerful and complex tool with many more features than what is covered here. We encourage you to explore the Git documentation and experiment with different Git commands to discover even more ways to use Git in your development projects.

Image description

Did I miss any other command or tip that you consider important? Please let me know in the comments. Thank you for taking the time to read this post!

Want to know more about git? Please follow this url: MDN Web Docs | Git and GitHub, it contains a getting started guide for Git and it most used hosting Github.

Top comments (0)