Welcome to my Git Commands Cheatsheet!
Command | Description | Examples |
---|---|---|
git clone | Downloads a copy of an existing Git repository | git clone https://github.com/user/repo.git |
git add | Stages changes for the next commit | git add file1.txt git add . (adds all modified files) |
git commit | Saves changes to the local repository | git commit -m "Added new feature" git commit -am "Fixed bug" (adds and commits modified files) |
git push | Sends commits to a remote repository | git push origin master git push (pushes to the default remote and branch) |
git pull | Retrieves and merges the latest changes from a remote repository | git pull origin master git pull (pulls from the default remote and branch) |
git branch | Creates, lists, or deletes branches | git branch git branch new-feature git branch -d old-feature |
git merge | Merges the changes from one branch into another | git merge new-feature |
git status | Shows the status of the local repository | git status |
git log | Shows the commit history of the repository | git log git log --oneline (shows a condensed version) |
git diff | Shows the differences between commits, commit and working tree, etc. | git diff git diff HEAD (compares the working tree with the latest commit) |
git stash | Saves changes that have not been committed to a temporary area | git stash git stash apply (applies the latest stash) |
git reset | Discards commits, moves the current branch, etc. | git reset --hard HEAD~1 (resets the repository to the previous commit and discards the latest commit) |
git revert | Reverses commits by creating new commits | git revert HEAD (reverts the latest commit) |
git fetch | Retrieves the latest changes from a remote repository without merging them | git fetch origin git fetch --all (fetches from all remotes) |
git rm | Removes files from the staging area and the file system | git rm file1.txt git rm -r folder (removes a file or a folder and its contents) |
Here are some scenarios where you would use these commands:
git clone
: You might use this command when you want to create a local copy of a repository that is hosted on a remote server, such as on GitHub or GitLab. For example, if you want to contribute to an open source project, you can usegit clone
to download the project’s repository to your local machine, make your changes, and then submit a pull request.git add
: You might use this command when you have made changes to a file or a group of files and you want to stage these changes for the next commit. For example, if you have modified several files in your project and you want to commit the changes, you can usegit add
to add the modified files to the staging area, and then usegit commit
to create a new commit with your changes.git commit
: You might use this command when you want to save your changes to the local repository. It is a good practice to commit your changes regularly, with clear and descriptive commit messages, so that you can track your progress and revert to previous versions if necessary.git push
: You might use this command when you want to send your commits to a remote repository, such as on GitHub or GitLab. This is especially useful if you are working on a project with other people and you want to share your changes with them.git pull
: You might use this command when you want to retrieve the latest changes from a remote repository and merge them into your local repository. This is useful when you are collaborating with other people on a project and you want to keep your local repository up to date with their changes.git branch
: You might use this command when you want to create a new branch to work on a new feature or a bug fix, or when you want to delete an old branch that is no longer needed. It is a good practice to use branches when working on larger projects, as it allows you to isolate your changes from the main branch and merge them when they are ready.git merge
: You might use this command when you want to merge the changes from one branch into another branch. This is typically used to merge the changes from a feature branch into the main branch, or to merge multiple branches into a release branch.git status
: You might use this command when you want to view the status of your local repository, including the modified files that have not been staged or committed, the current branch, and the commit history. This command is helpful for keeping track of your progress and identifying any conflicts that might need to be resolved.git log
: You might use this command when you want to view the commit history of the repository. This is useful for understanding the changes that have been made to the project over time, and for finding specific commits by message or author.git diff
: You might use this command when you want to view the differences between commits, the working tree and the staging area, or the working tree and a specific commit. This is helpful for identifying what has changed between different versions of the project, and for resolving conflicts during a merge.git stash
: You might use this command when you have made changes to your working tree that you want to temporarily save without committing them. This is useful when you need to switch branches or switch to a different task, but you don’t want to lose your changes.
git reset
: You might use this command when you want to discard commits or move the current branch to a previous commit. This is useful when you want to discard the latest changes that you made by mistake, or when you want to move the branch to a specific commit in order to start a new line of development.git revert
: You might use this command when you want to reverse commits by creating new commits that undo the changes made in the original commits. This is useful when you want to revert a series of commits that introduced a bug or made unwanted changes, but you don’t want to permanently delete the commits from the history.git fetch
: You might use this command when you want to retrieve the latest changes from a remote repository without merging them into your local repository. This is useful when you want to see what changes have been made by other people without modifying your local repository, or when you want to download new branches or tags from the remote repository.git rm
: You might use this command when you want to remove files from the staging area and the file system. This is useful when you want to delete a file that is no longer needed, or when you want to unstage a file that you accidentally added to the staging area.
I hope these examples give you an idea of how each Git command can be used in different scenarios.