Git Cheat Sheet

  • Git is decentralized: every clone of a Git repository when in sync is an exact copy of the parent repository containing all of the history.
  • Setup a Git repository by creating a .git directory:
    $ git init [--bare -> without a working copy]
  • Stop the Git tracking and history by removing the .git directory.
  • If there is a file named .gitignore in the same directory as the .git directory, its contents (file paths) will be ignored by Git.
  • Workflow:
                 Git repository
                  /           \
                 /             \
                /               \
         changes in             staging
        the working   --------   area
         directory

  • Print the names of the changed files in the working directory and the names of the staged files in the staging area:
    $ git status
  • Show differences between the Git repository and the working directory:
    $ git diff
  • Remove changes from the working directory:
    • Remove the changes in a file or adds the file in the working directory back to the state after the latest commit in the Git repository:
      $ git checkout $filepath
    • Remove all changes in the working directory back to the state after the latest commit in the Git repository (doesn’t affect the staging area):
      $ git clean -df; git checkout .
  • Stage changes from the working directory:
    • Stage everything (call in the root directory):
      $ git add .
    • Stage for a file to be untracked by Git, but don’t remove it from the working directory:
      $ git rm --cached $filepath
  • Remove staged changes:
    • Remove changes in a file from the staging area back to the working directory:
      $ git reset HEAD $filepath
    • Remove everything from the staging area back to the working directory:
      $ git reset
  • Remove all changes from the working directory and the staging area:
    $ git reset --hard
  • Commit staged changes to the Git repository:
    $ git commit -m "$commit-message"
  • Print the branch’s commit history:
    $ git log [--stat -> which files were affected] [--pretty=oneline -> one line for main information per commit] [--graph -> shows branching]
  • Revert a commit (by creating a new commit and not modifying the history):
    $ git revert $commit-hash
  • Modify the history (only if you don’t care that if a remote branch exists it gets corrupted). The modification of one older commit causes modification of all commits newer than it.
    • Open a rebase TODO file (options: modify commit messages of, edit, squash, remove and reorder commits):
      $ git rebase -i $oldest-commit-to-be-shown-hash^
      $ git rebase -i HEAD~$number-of-last-commits-to-be-shown
      Show all commits:
      $ git rebase -i --root
      When editing a commit:
      stage changes then execute:
      $ git commit --amend
      $ git rebase --continue
    • A quicker way to delete some of the last commits:
      $ git reset [--soft -> moves the differences to the staging area / --mixed (default) -> moves the differences to the working directory / --hard -> completely discards the changes] $oldest-commit-not-to-be-deleted-hash
    • if there is a remote branch, after the modification of the history execute:
      $ git push -f
  • Print all local branches:
    $ git branch
  • Print all the branches merged into the current branch:
    $ git branch --merged
  • Create a new branch from the current branch:
    $ git branch $branch-name
  • Checkout another branch:
    $ git checkout $branch-name
  • Create a new branch from the current branch and check it out:
    $ git checkout -b $branch-name
  • Delete a branch:
    $ git branch -D $branch-name
  • Get all commits from another branch that are not in the current branch into the current branch (if there are commits in the current branch that are not in the other branch):
    • Without modifying the branch’s history and by creating an additional merge commit:
      $ git merge $another-branch-name
    • By modifying the branch’s history by moving the branching point to after
      the last commit contained in both branches which modifies all subsequent commits:
      $ git rebase $another-branch-name
      If there are conflicts:
      fix them, then stage them, then execute:
      $ git rebase --continue
  • Copy a specific commit from another branch (cherry-pick it) into the current branch:
    $ git cherry-pick $commit-hash
  • Clone an existing Git repository (can be locally as well):
    $ git clone $git-repository-URL $clone-location
  • Print the remote repository location:
    $ git remote -v
  • Print all branches (including the remote ones):
    $ git branch -a
  • Get new remote data (without integrating it in the working directory):
    $ git fetch
  • Get new remote data and integrate it in the working directory:
    $ git pull
  • Push all data to the remote repository:
    $ git push
  • Link the current local branch with a remote branch (will create the remote branch if it doesn’t exist):
    $ git push -u $remote-repository-name $remote-branch-name
  • Delete a remote branch:
    $ git push $remote-repository-name -d $remote-branch-name
  • Print remote tags:
    $ git ls-remote -t
  • Push a tag to a remote repository:
    $ git push origin $tag-name
  • Delete a remote tag:
    $ git push -d $remote-repository-name $tag-name
  • Copy the changes (not new files) from the working directory to a stash with a certain name and remove them from the working directory:
    $ git stash save $stash-name
  • Print all stashes:
    $ git stash list
  • Apply the changes from the stash to the working directory and keep the stash:
    $ git stash apply $stash-hash
  • Copy the changes (not new files) from the working directory to a stash and remove them from the working directory:
    $ git stash
  • Apply the changes from the last saved stash to the working directory and delete the stash:
    $ git stash pop
  • Delete a stash:
    $ git stash drop $stash-hash
  • Delete all stashes:
    $ git stash clear
  • Show all tags:
    $ git tag
  • Create a tag:
    $ git tag $tag-name
  • Checkout a tag:
    $ git checkout $tag-name
  • Delete a tag:
    $ git tag -d $tag-name
  • Print past local changes of HEAD (commits, branches checkouts, resets, rebases) which are stored temporarily:
    $ git reflog $detached-head-state-hash
  • Switch to a detached HEAD state:
    $ git checkout $detached-head-state-hash
  • When in a detached HEAD state:
    • Reset hard to a previous HEAD state:
      $ git reset --hard $detached-head-state-hash
    • Create a branch to retain it:
      $ git branch $branch-name

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s