logo
Published on

Git / GitHub Cheatsheet

Authors
  • avatar
    Name
    seren-wib
    Twitter
Contents

0. Introduction

A personal cheatsheet for when I get confused. Run the commands in a terminal after moving into the repository folder.

In restaurant terms, the working folder on my computer is the kitchen and the repository on GitHub is the head-office storeroom. A photo of a finished plate is a record of one change, and setting a plate that is still being cooked into a drawer for a moment is putting work aside. That is, the kitchen is the local repository, the storeroom is the remote called origin, the photo is a commit, and the drawer is a stash.

This post covers checking status, a pull that loses no changes, resolving conflicts, committing and pushing, and undoing changes.


1. Checking the Current State

git status              # which files changed
git log --oneline -10   # the 10 most recent commits
git fetch               # only fetches remote info (files don't change)
git log HEAD..@{u} --oneline   # commits that are on the remote but not on mine

2. Pulling Without Losing Changes

What I did on 2026-06-13. When I have files being edited locally and the remote also has new commits. A plain git pull stops or is refused on overlapping files. The safe order:

# 1. Put local changes away in the drawer (-u also includes newly created untracked files)
git stash push -u -m "stash before pull"

# 2. Fetch the remote from the clean state
git pull --ff-only

# 3. Take the stashed changes back out
git stash pop
  • If files overlap, stash pop produces a conflict → go to section 3 below.
  • No data is lost. Even on a conflict, the stash stays in git stash list.

3. Resolving Conflicts

When you open a conflicted file, it contains markers like these:

<<<<<<< Updated upstream
Remote (fetched side) content
=======
My local (stash) content
>>>>>>> Stashed changes

How to resolve:

  1. Keep only the content you want and delete the three marker lines yourself (<<<, ===, >>>)
  2. If both sides are needed, keep both and delete only the markers
  3. Stage the cleaned-up file:
git add "path/to/file"

Delete vs modify conflict (one side deleted the file and the other edited it):

git rm "path/to/file"      # when choosing deletion
# or
git add "path/to/file"     # when choosing to keep it (the file stays)

Once all conflicts are cleaned up, empty the stash drawer:

git stash drop         # delete the stash just popped
git stash list         # check the remaining stashes

4. Committing and Pushing

git add .                    # stage all changes (or just "file name")
git commit -m "message"        # commit
git push                     # upload to the remote (GitHub)

5. Undoing (When You Make a Mistake)

git restore "path/to/file"           # discard edits and go back to the last commit's state
git restore --staged "path/to/file"  # undo an add (changes are kept)
git stash list               # list of stashed changes
git stash pop                # take the most recent stash back out
git reset --hard HEAD        # ⚠️ discards all local changes (cannot be undone, careful)

6. Reflection

Even on a conflict the stash stays in git stash list, so no data is lost.