Home DevOps Git Commands Cheatsheet

Git Commands Cheatsheet

by admin

Welcome to my Git Commands Cheatsheet!

CommandDescriptionExamples
git cloneDownloads a copy of an existing Git repositorygit clone https://github.com/user/repo.git
git addStages changes for the next commitgit add file1.txt git add . (adds all modified files)
git commitSaves changes to the local repositorygit commit -m "Added new feature" git commit -am "Fixed bug" (adds and commits modified files)
git pushSends commits to a remote repositorygit push origin master git push (pushes to the default remote and branch)
git pullRetrieves and merges the latest changes from a remote repositorygit pull origin master git pull (pulls from the default remote and branch)
git branchCreates, lists, or deletes branchesgit branch git branch new-feature git branch -d old-feature
git mergeMerges the changes from one branch into anothergit merge new-feature
git statusShows the status of the local repositorygit status
git logShows the commit history of the repositorygit log git log --oneline (shows a condensed version)
git diffShows the differences between commits, commit and working tree, etc.git diff git diff HEAD (compares the working tree with the latest commit)
git stashSaves changes that have not been committed to a temporary areagit stash git stash apply (applies the latest stash)
git resetDiscards commits, moves the current branch, etc.git reset --hard HEAD~1 (resets the repository to the previous commit and discards the latest commit)
git revertReverses commits by creating new commitsgit revert HEAD (reverts the latest commit)
git fetchRetrieves the latest changes from a remote repository without merging themgit fetch origin git fetch --all (fetches from all remotes)
git rmRemoves files from the staging area and the file systemgit 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 use git 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 use git add to add the modified files to the staging area, and then use git 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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More