Skip to content

Instantly share code, notes, and snippets.

@luszczynski
Last active May 11, 2019 17:38
Show Gist options
  • Save luszczynski/5486cb8f748ba367beb87712ec37dcca to your computer and use it in GitHub Desktop.
Save luszczynski/5486cb8f748ba367beb87712ec37dcca to your computer and use it in GitHub Desktop.
Git Cheat Sheet - Basic Commands for daily use

Git Cheat Sheet

Basics

Working ------ git add -----> Stage Index ------ git commit -----> Repository

  • Git generates a checksum for every change set (commit)
    • Example: 987e06a6d47b724223b40c2ccda229c58fa0b63f

HEAD

HEAD points to the latest commit in our repository (not stage and not working)

cat .git/HEAD
cat .git/refs/heads/master

Initialize Repository

git init

Git add files

Add files from working tree to stage index

git add file.txt

Git diff

Changes in working tree

This will show the diff between repository vs working tree

git diff file.txt

Changes in stage index

This will show the diff between repository vs stage index

git diff --staged

Git delete

git rm file.txt

Git Move or Delete

git mv file.txt file2.txt

View Commit Log

git log

View only a specific number of commit

git log -n 5

View Commit Log by Author

git log --author="Gustavo"

You don't need to use de full author's name.

View CommitLog by date

git log --since=2018-05-05
git log --until=2018-05-05

Grep Commit Log

git log --grep="Init"

Undoing Changes

Undoing working directory changes

Make the working directory file index.html look like the same version that is in my repository

git checkout -- index.html

Undoing changes to the stage index (unstaging files)

Change the index.html to the last commit (HEAD) of our current branch/repository (master)

git reset HEAD index.html

Amending commits (undoing commits)

It is possible to change the last commit.

git add file_to_be_included_to_last_commit.html
git commit --amend -m "forgot to add this file to the last commit"
git log

Retrieving old versions

We can go back a file to a specific commit. First we need to know which commit we would like to go back using git log.

git log
git checkout 987e06a6d47b724223b40c2ccda229c58fa0b63f -- index.html

Reverting a commit

Git revert create a new commit with the commit we choose from git log

git log
git revert 987e06a6d47b724223b40c2ccda229c58fa0b63f

Undo many commits

git reset allow to specifie where the HEAD pointershould point to.

  • --soft: does not change stagin index or working directory
  • --mixed (default): changes staging index to match repository but does not change working directory
  • --hard: changes staging index and working directory to match repository
Soft Reset
git reset --soft 987e06a6d47b724223b40c2ccda229c58fa0b63f
Mixed Reset
git reset --mixed 987e06a6d47b724223b40c2ccda229c58fa0b63f
Hard Reset
git reset --hard 987e06a6d47b724223b40c2ccda229c58fa0b63f

Remove untracked file from workind directory

# Check what would happen
git clean -n
# Remove untracked file that are not in stage index
git clean -f

Gitignore

We need to create a file called .gitignore inside our root repo.

Ignore files globally

git config --global core.excludesfile /etc/.gitignore_global

.gitignore_global use the same rules applied to .gitignore

Ignore tracked files

Git will not ignore files that have already been tracked before the creation of .gitignore

# We can remove the file from repository and original
git rm index.html

# If we want to keep the file in our repository
# We remove the file only from staging index
git rm --cached index.html

Tracking empty dirs

Git ignores empty dirs

touch my_tracked_dir/.gitkeep

Navigating the commit tree

tree-ish

We can reference a commit by using:

  • full SHA-1 hash
  • short SHA-1 hash
    • at least 4 chars
    • from 8 to 10 chars
  • HEAD pointer
  • branch reference, tag reference
  • ancestry
    • Parent commit
      • HEAD^
      • acf87504^
      • master^
      • HEAD1, HEAD
    • Grandparent commit
      • HEAD^^
      • acf87504^^
      • master^^
      • HEAD~2

List the tree

git ls-tree HEAD
git ls-tree master
git ls-tree master index.html
git ls-tree master^ index.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment