trusted

Git cheatsheet

The tutorial provides a quick guide to Git commands and their usage. It is designed to help beginners understand the basic Git commands and their functions. It also provides a brief overview of the importance of using Git in software development.

Recent posts

...
Git cheatsheet

The tutorial provides a quick guide to Git commands and their usage. It is designed to help beginner...

26 May 2024

Continue reading
...
Deploy your static website on Netlify for free

The tutorial provides a step-by-step guide to deploy a static website on Netlify for free. It is des...

23 April 2024

Continue reading

Yasir Mansoori

MERN Developer || Full Stack Developer Enthusiast || Software Developer Enthusiast || CS undergrad at SRM University || Lead @GFG-SRMIST

Last Updated - 26 May 2024

Table of Contents


What is Git ?

Git is the free and open source distributed version control system that's responsible for everything GitHub related that happens locally on your computer. This cheat sheet features the most important and commonly used Git commands for easy reference.

  • Easy setup
  • Collaboration friendly
  • Fast
  • Open source

Understanding Version Control

Version control, also known as source control, is the technique of tracking and managing changes to codes and these are the systems that are software tools that enable software teams to manage modifications to source code as time passes.

What is GitHub?

GitHub is a widely-used Free-to-use cloud Storage platform with version control and many other essential features that specifically helps developers to manage and deploy their projects on GitHub.

Installation

With platform specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.

Download the latest version of Git for Windows

  • Step 1: Extract and Launch Git Installer
  • Step 2: Go to your download location and double-click the file to launch the installer.
  • Step 3: Allow the app to modify your device by selecting Yes in the User Account Control window that appears.
  • Step 4: Check the GNU General Public License and click Next.
  • Step 5: Select the install location. If you don’t have a reason to modify it, leave it to default and click Next.
  • Step 6: A screen for component selection will display. Leave the settings as it is and click Next.
  • Step 7: The installer asks you to create a start menu folder. Simply click Next.
  • Step 8: Choose the text editor you want to use with Git and click Next.
  • Step 9: The following step allows you to give your original branch a new name. ‘Master’ is the default. Leave the default choice selected and press the Next button.
  • Step 10: You can adjust the PATH environment during this installation phase. When you run a command from the command line, the PATH is the default set of folders that are included. Continue by selecting the middle (recommended) option and clicking Next.
  • Step 11: The following option concerns server certificates. The default choice is used by the majority of users. Simply click Next.
  • Step 12: This step deals with how data is structured, and altering this option may create issues. So, it is advised to leave the default selection.
  • Step 13: Select the terminal emulator that you wish to use. Because of its features, the default MinTTY is suggested. Click Next.
  • Step 14: The installer now prompts you to specify what the git pull command should perform. Leave the default selected option and click Next.
  • Step 15: The next step is to decide which credential helper to employ. Credential helpers are used by Git to retrieve or save credentials. Leave the default selection and click Next.
  • Step 16: Although the default choices are suggested, this step allows you to select which additional features to activate.
  • Step 17: Git offers to install some experimental features. Leave them unchecked and click Install.
  • Step 18: Once the installation is complete, launch the Git bash.

Benefits of Using Git

  • History Tracking: Git allows you to track every change made in your project, including: who made the change and when it was made.
  • Collaboration: Multiple developers can be able work on the same project at the same time, and Git efficiently manages the merging of changes in code.
  • Branching and Merging: Git enables developers to create branches to work on new features or bug fixes and later merge them back into the main codebase.
  • Offline Work: Git works offline, which means you can commit changes and work on your project even without an internet connection.

Initializing a Repository

Here are the Git initializing a repository commands:

  1. Initializes a new Git repository in the current directory.
  2. git init
  3. Creates a new Git repository in the specified directory.
  4. git init [directory]
  5. this Clones a repository from a remote server to your local machine.
  6. git clone [url]
  7. Clones a specific branch from a repository.
  8. git clone -b [branch] [url]

Basic Git Commands

Here are some basic Git commands:

  1. Adds a specific file to the staging area.
  2. git add <file>
  3. Adds all modified and new files to the staging area.
  4. git add .
  5. Shows the current state of your repository, including tracked and untracked files, modified files, and branch information.
  6. git status
  7. Displays ignored files in addition to the regular status output.
  8. git status -u
  9. Shows the changes between the working directory and the staging area (index).
  10. git diff
  11. Displays the differences between two commits.
  12. git diff [commit] [commit]
  13. Displays the changes between the staging area (index) and the last commit.
  14. git diff --staged
  15. Display the difference between the current directory and the last commit
  16. git diff HEAD
  17. Creates a new commit with the changes in the staging area and opens the default text editor for adding a commit message.
  18. git commit
  19. Creates a new commit with the changes in the staging area and specifies the commit message inline.
  20. git commit -m "message"
  21. Commits all modified and deleted files in the repository without explicitly using git add to stage the changes.
  22. git commit -a
  23. Creates a new note and associates it with an object (commit, tag, etc.).
  24. git notes add -m "note"
  25. Restores the file in the working directory to its state in the last commit.
  26. git restore <file>
  27. Moves the branch pointer to a specified commit, resetting the staging area and the working directory to match the specified commit.
  28. git reset [commit]
  29. Moves the branch pointer to a specified commit, preserving the changes in the staging area and the working directory.
  30. git reset --soft [commit]
  31. Moves the branch pointer to a specified commit, discarding all changes in the staging area and the working directory, effectively resetting the repository to the specified commit.
  32. git reset --hard [commit]
  33. Removes a file from both the working directory and the repository, staging the deletion.
  34. git rm <file>
  35. Moves or renames a file or directory in your Git repository.
  36. git mv <source> <destination>

Git Commit (Updated Commands)

Here are some of the updated commands for Git commit:

  1. Create a new commit in a Git repository with a specific message to indicate a new feature commit in the repository.
  2. git commit -m "feat: message"
  3. Create a new commit in a Git repository with a specific message to fix the bugs in codebases
  4. git commit -m "fix: message"
  5. Create a new commit in a Git repository with a specific message to show routine tasks or maintenance.
  6. git commit -m "chore: message"
  7. Create a new commit in a Git repository with a specific message to change the code base and improve the structure.
  8. git commit -m "refactor: message"
  9. Create a new commit in a Git repository with a specific message to change the documentation.
  10. git commit -m "docs: message"
  11. Create a new commit in a Git repository with a specific message to change the styling and formatting of the codebase.
  12. git commit -m "style: message"
  13. Create a new commit in a Git repository with a specific message to indicate test-related changes.
  14. git commit -m "test: message"
  15. Create a new commit in a Git repository with a specific message to indicate performance-related changes.
  16. git commit -m "perf: message"
  17. Create a new commit in a Git repository with a specific message to indicate the continuous integration (CI) system-related changes.
  18. git commit -m "ci: message"
  19. Create a new commit in a Git repository with a specific message to indicate the changes related to the build process.
  20. git commit -m "build: message"
  21. Create a new commit in a Git repository with a specific message to indicate the changes related to revert a previous commit.
  22. git commit -m "revert: message"

Branching and Merging

Here are some branching and merging commands:

  1. Lists all branches in the repository.
  2. git branch
  3. Creates a new branch with the specified name.
  4. git branch <branch>
  5. Deletes the specified branch.
  6. git branch -d <branch>
  7. Lists all local and remote branches.
  8. git branch -a
  9. Lists all remote branches.
  10. git branch -r
  11. Switches to the specified branch.
  12. git checkout <branch>
  13. Creates a new branch and switches to it.
  14. git checkout -b <branch>
  15. Discards changes made to the specified file and revert it to the version in the last commit.
  16. git checkout -- <file>
  17. Merges the specified branch into the current branch.
  18. git merge <branch>
  19. Displays the commit history of the current branch.
  20. git log
  21. Displays the commit history of the specified branch.
  22. git log <branch>
  23. Displays the commit history of a file, including its renames.
  24. git log --follow <file>
  25. Displays the commit history of all branches.
  26. git log --all
  27. Stashes the changes in the working directory, allowing you to switch to a different branch or commit without committing the changes.
  28. git stash
  29. Lists all stashes in the repository.
  30. git stash list

Remote Repositories

Here are some remote repository commands:

  1. Retrieves change from a remote repository, including new branches and commit.
  2. git fetch
  3. Retrieves change from the specified remote repository.
  4. git fetch <remote>
  5. Fetches changes from the remote repository and merges them into the current branch.
  6. git pull
  7. Fetches changes from the specified remote repository and merges them into the current branch.
  8. git pull <remote>
  9. Pushes local commits to the remote repository.
  10. git push
  11. Pushes local commits to the specified remote repository.
  12. git push <remote>
  13. Pushes local commits to the specified branch of the remote repository.
  14. git push <remote> <branch>
  15. Pushes all branches to the remote repository.
  16. git push --all
  17. Lists all remote repositories.
  18. git remote
  19. Adds a new remote repository with the specified name and URL.
  20. git remote add <name> <url>

Git Comparison

Here are some Git comparison commands:

  1. Shows the details of a specific commit, including its changes.
  2. git show <commit>
  3. Shows the details of the specified commit, including its changes.
  4. git show <commit> --name-only

Conclusion

In conclusion, This Git Cheat Sheet is thoughtfully organized and categorized, making it easy for developers to quickly find the commands they need for specific use cases. Whether it’s configuring and setting up Git, creating and managing projects, taking snapshots, branching and merging, sharing and updating, comparing changes, or managing version history, the Git Cheat Sheet covers it all.

By utilizing this resource, developers can enhance their productivity and efficiency in working with Git, ultimately leading to smoother and more successful software development projects.

Resources :

Don't watch the clock; do what it does. Keep going.
- Sam Levenson