Skip to content

Instantly share code, notes, and snippets.

@macton
Last active April 25, 2023 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macton/f246b26dc1736ccf98ade176285a094e to your computer and use it in GitHub Desktop.
Save macton/f246b26dc1736ccf98ade176285a094e to your computer and use it in GitHub Desktop.

Git cheatsheet

See also:

Quickstart for github

Config ssh keys for github See: https://help.github.com/articles/generating-ssh-keys
$ ssh-keygen -t ed25519 -C "your_email@youremail.com"
$ cat /home/macton/.ssh/id_ed25519.pub

Copy and paste to https://github.com/settings/ssh

Config git 
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
$ git config --global push.default matching
$ git config --global url.ssh://git@github.com/.insteadOf https://github.com/

Git concepts

  • Repository - Collection of commit history of files being managed (not directories)
  • Working directory - Files in local file system (may or may not be tracked)
  • .git folder - Actual repository (full history)
  • Commit - Snapshot of change(s)
  • Branch - Timeline of commits
  • Staging Area - Pending commits
  • States - Working directory -> Staging Area -> Repository (-> Remote)
  • Remote - Conceptually additional State where commits are made to remote repository (Having its own three stages)

Local repositories

Initialize name

  git config --global user.name "Your Name"
  git config --global user.email "your.email@your-place.com"

Set editor

  git config core.editor "notepad++ -multiInst -nosession"

Test editor

  git config --global -e

Configure diff tool (p4merge)

  git config --global diff.tool p4merge
  git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
  git config --global difftool.prompt false

Configure merge tool (p4merge)

  git config --global merge.tool p4merge
  git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
  git config --global mergetool.prompt false

Create project

  git init project_name

Status

  git status

Add file(s) to staging area

  git add <files>

Commit files to repo

  git commit -m "commit message"

Show list of commits

  git log

Show list of commits, one line each + asterix graph of branching hierarchy + which commits part of which branches + history of all branches

  git log --oneline --graph --decorate --all   

Show list of commits and diffs

  git show

List tracked files

  git ls-files

Add modified files and commit

  git commit -am "message"

Unstage file

  git reset HEAD <file>...

Discard changes in working director

  git checkout -- <file>...

Add git command alias globally

  git config --global alias.hist "log --oneline --graph --decorate --all" 

List global values

  git config --global --list

Git log (based on alias above)

  git hist

Git log of single file

  git hist -- README.md

Rename file

  git mv example.txt demo.txt

Remove file

  git rm demo.txt

Stage changed files

  git add -u

Stage changed files (all types of changes)

  git add -A

Exclude files from repository edit .gitignore one file expression per line to ignore

List branches

  git branch

List branch (both remote and local branches)

  git branch -a

Create and switch to new branch (stage open changes in new branch)

  git checkout -b updates

Compare changes between branches

  git diff updates master

Switch branch

  git checkout master

Merge changes from another branch

  git merge updates

Delete branch

  git branch -d updates

Run mergetool

  git mergetool

Tag (lightweight) current commit

  git tag mytag

List all tags

  git tag --list

Delete tag

  git tag -d mytag

Tag (annotated) tag

  git tag -a v1.0 -m "Release 1.0"

Show list of commits and diffs (for given annotated tag)

  git show v1.0

Stash current tracked changes

  git stash

List currently stashed changes

  git stash list

Reapply last stashed changes and remove stash

  git stash pop

Reapply previous commit (change where HEAD is)

  git reset dadad3a --soft 

Git resets are --soft, --mixed, --hard (--mixed is default) What are the differences? See: http://stackoverflow.com/a/3528483 --soft Moves changes to staging --mixed Moves changes to working directory --hard Removes changes destructively

Show list of commits, one line each (up to HEAD)

  git log --oneline

Show list of all actions taken in repo

  git reflog

Remote repositories

Show all connected remote repositories associated with current repository

  git remote -v

Connect repository to remote repository with name "origin"

  git remote add origin ssh://git@bitbucket.org/macton/demo.git

Push all commits from local branch "master" to remote repository "origin" with tracking relationship -u (create equivalent "master" branch on remote), include tags --tags

  git push -u origin master --tags

Remove connection with remote repository named "origin"

  git remote rm origin 

BitBucket vs GitHub

BitBucket GitHub
Unlimited public repos Unlimited public repos
Unlimited private repos Charge by private repos
Charge by team size Unlimited team size
Git repos Git repos
Pull requests Pull requests
Snippets Gist
Teams Organizations
Internal Issues
JIRA integration Third party
Atlassian Integration Repo initialization
Mercurial Social features

Clone remote repository

  git clone git@github.com:macton/start-remote.git

Clone remote repository into specified folder "demo-github"

  git clone git@github.com:macton/start-remote.git demo-github

Sync references between local ("master") and remote repository ("origin") without merging commits

  git fetch origin master

git fetch vs git pull fetch adds remote history to repository, but does not merge pull does fetch then merge See: http://stackoverflow.com/a/292359

Fetch and merge changes from remote repo "origin" into local branch "master"

  git pull origin master

Synchronize and update remote repo "origin" from local branch "master"

  git push origin master

Other tips

$ git config merge.ours.driver true
e.g Create a file named .gitattributes in the same dir, with this line: config.php merge=ours
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment