I created this Git cheat sheet specifically for Technical Writers, and it includes only the commands that we use regularly.

Branch
  • git branch: List all of the branches in your repository
  • git branch "branch": Create a new branch called "branch". This does not check out the new branch.
  • git branch -D "branch": Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.
  • git branch -D "branch": Force delete the specified branch, even if it has unmerged changes. This is the command to use if you want to permanently throw away all of the commits associated with a particular line of development.
  • git branch -m "branch": Rename the current branch to <branch>.
  • git branch -a - List all remote branches.
Files
  • git add file: This command stages a file for commit, adding it to the list of changes that will be committed when you run the git commit command.
  • git rm file: This command removes a file from the repository and stages the removal for commit.
  • git reset file: To remove a file that has been staged for commit in Git. Replace file with the name of the file you want to unstage. When you run this command, the file will be removed from the staging area and left in the working directory, but it will not be committed.
  • git mv old_file new_file: This command renames a file and stages the rename for commit.
  • git checkout -- file: This command discards changes to a file, reverting it to the version in the most recent commit.
  • git commit -a: This command commits all tracked changes, including modifications and deletions, but does not add new files that have not yet been staged.
  • git stash: This command temporarily saves changes that have not yet been staged and clears the staging area, allowing you to switch branches or restore the repository to a clean state.
  • git stash apply: This command restores changes that were previously stashed using the git stash command.
Commit
    Generic Commit
    • git commit: This command is used to commit changes to the local repository.
    • git commit -a: This command commits all tracked changes, including modifications and deletions, but does not add new files that have not yet been staged.
    • git commit -m "message": This command commits changes and includes a commit message describing the changes.
    • git commit --amend: This command can be used to modify the most recent commit by adding to it or changing the commit message.
    • git commit --amend -m "new message": This command can be used to modify the most recent commit and include a new commit message.
    • git commit --no-edit: This command can be used to commit changes and reuse the commit message from the previous commit.
    Rolling back a commit
    • git revert commit_id: This command creates a new commit that undoes the changes made in the specified commit. It does not permanently delete the commit, but rather creates a new commit that undoes the changes made in the original commit. You can run `git log` find the commit id.
    • git reset "branch": This command moves the branch pointer to a previous commit and discards commits that occurred after that commit. It does not delete the commits permanently, but rather removes them from the current branch and places them in a separate "dangling" state where they are not accessible from any branch. You can also run `git log` find the commit id.
    • git rebase -i "branch": This command opens an interactive rebase session, allowing you to edit, delete, or merge commits. It can be used to delete commits by removing them from the list of commits shown in the interactive rebase session. You can also run `git log` find the commit id.
    • git push --force: This command can be used to delete commits that have already been pushed to a remote repository. It works by forcibly replacing the remote repository's history with the local repository's history. This is generally considered a risky operation and should be used with caution.
    Cherrypick a commit
    • git cherry-pick "branch" - Cherry-pick a commit in Git. This command applies the changes made in a specific commit to the current branch, without merging the entire branch or merging the commit's parent commits. You can also run `git log` find the commit id.
Course completed
35%

Have an issue? Please provide specific feedback by reporting an issue.