Git Cheatsheet

The top 10 most common git commands along with examples are listed below. Random fact – Git stands for Global Information Tracker. 

Summary of the most common Git commands

  1. git init: Initializes a new Git repository.

  2. git clone: Makes a copy of an existing Git repository.

  3. git add: Adds a file to the staging area.

  4. git commit: Saves a snapshot of the staging area to the repository.

  5. git diff: Shows the differences between file versions.

  6. git log: Shows the commit history for the repository.

  7. git branch: Manages branches in the repository.

  8. git merge: Merges branches together.

  9. git push: Sends local commits to the remote repository.

  10. git pull: Updates the local repository with commits from the remote repository.

Common Git commands with examples

  1. git init: This command is used to create a new local Git repository. For example, if you have a project directory that you want to track with Git, you would navigate to that directory in your terminal and run git init.

  2. git clone: This command is used to make a copy of an existing Git repository. For example, if you want to clone the repository for a project that you want to contribute to, you would run git clone https://github.com/user/project.git, where “https://github.com/user/project.git” is the URL of the remote repository.

  3. git add: This command is used to add a file to the staging area. For example, if you have made changes to a file called “main.c” and you want to add those changes to the staging area, you would run git add main.c.

  4. git commit: This command is used to save a snapshot of the staging area to the repository. For example, after you have added all of the necessary files to the staging area, you would run git commit -m "Added new feature" to commit those files to the repository, including a commit message that describes the changes you made.

  5. git diff: This command is used to show the differences between file versions. For example, if you want to see what changes you have made to a file called “main.c” since the last commit, you would run git diff main.c.

  6. git log: This command is used to show the commit history for a repository. For example, if you want to see a list of all commits made to a repository, you would run git log.

  7. git branch: This command is used to manage branches in a repository. For example, if you want to create a new branch called “feature”, you would run git branch feature.

  8. git merge: This command is used to merge branches together. For example, if you have made changes to the “feature” branch and you want to merge those changes back into the main branch, you would run git merge feature.

  9. git push: This command is used to send local commits to the remote repository. For example, if you want to push your local commits to the remote repository on GitHub, you would run git push origin main, where “origin” is the name of the remote repository and “main” is the name of the branch you want to push.

  10. git pull: This command is used to update the local repository with commits from the remote repository. For example, if you want to update your local repository with the latest commits from the remote repository, you would run git pull origin main, where “origin” is the name of the remote repository and “main” is the name of the branch you want to pull.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.