Skip to content

Instantly share code, notes, and snippets.

@vpiotr
Last active November 8, 2022 23:06
Show Gist options
  • Save vpiotr/c3b9da559b6fb40f2b37c0e0c241eb4a to your computer and use it in GitHub Desktop.
Save vpiotr/c3b9da559b6fb40f2b37c0e0c241eb4a to your computer and use it in GitHub Desktop.
Git Command Quickref

Configure

  • show config value

    git config --global user.name
    
  • edit all config values

    nano ~/.gitconfig
    

SSH config

  • generate keys - see generating a new ssh key

    ssh-keygen -t ed25519 -C "your_email@example.com"
    
  • copy pub key to clipboard

    cat ~/.ssh/id_ed25519.pub 
    
  • or

    cat ~/.ssh/id_rsa.pub
    
  • start SSH agent (Linux)

    eval "$(ssh-agent -s)"
    
  • add key to local ssh agent

    ssh-add ~/.ssh/id_ed25519		
    
  • add key to GitHub

See Adding a new SSH key to your github account.

  • activate access to GitHub via ssh

    ssh -T git@github.com
    
    (select "yes")
    

Initialize

  • clone repo via ssh (if configured)

    git clone git@github.com:jquery/jquery.git
    
  • clone repo via HTTPs

    git clone https://github.com/jquery/jquery.git
    
  • make new dir and init

    git init foo
    
  • init repo in current dir

    git init
    

Add

  • add single file to staging

    git add about.html
    
  • add all files to staging

    git add .
    

Change executable flag

  • list files with executable/read/write flags

    git ls-files --stage
    
  • activate executable flag (from 644 to 755)

    git update-index --chmod=+x <file-name>
    ex. git update-index --chmod=+x foo.sh
    

Commit

  • interactive commit

    git commit --interactive
    
  • commit old but not staged

    git commit -a
    
  • commit with editor for message

    git commit
    
  • commit with message from CLI

    git commit -m "Commit message"
    
  • add and commit

    git commit -am "Add more lives"
    

Log

  • show log of changes

    git log
    
  • show log (compact format)

    git log --oneline
    
  • or

    git log --pretty --oneline
    
  • show modified files in log

    git log --stat
    
  • show log with single comment and modified files

    git log --oneline --stat
    
  • show changes (diff) for entries in log

    git log --patch
    
  • show only 3 last log entries

    git log -3
    
  • filter entries basing on dates

    git log --since="2018-08-15"
    git log --until="2018-08-15"
    git log --since="2 days ago"
    
  • filter entries basing on commit message

    git log --grep="paddle"
    
  • graph of changes for branches

    git log --graph
    

Diff

  • show introduced changes except staged

    git diff
    
  • show introduced changes in staging area

    git diff --staged
    
  • show differences between current and past version of file

    git diff e2fdef3ba engine.js
    
  • show differences between 2 versions of file

    git diff e2fdef3ba b732187 engine.js
    

Current status

  • show list of modified files

    git status
    

Checkout

  • retrieve selected revision from repo to local copy

    git checkout f3121788
    
  • return to current revision

    git checkout master
    
  • verify which revision is current

    cat .git/HEAD
    

Branch

  • show available branches

    git branch
    
  • or

    ls .git/refs/heads
    
  • list branches with latest changes

    git branch -vv
    
  • create new branch

    git branch new_branch
    
  • switch to branch

    git checkout new_branch
    
  • return to "trunk"

    git checkout master
    
  • delete branch

    git branch -d new_branch
    
  • create branch and switch to it

    git checkout -b new_branch
    
  • merge changes from branch to master

    git checkout master
    git merge hotfix-001
    
  • correct merge conflict

    git mergetool variables.js
    
  • apply branch changes to new base

    git checkout new_feature
    git rebase master
    
  • cherry pick - merge single commit

    git cherry-pick e6f4c858af
    
  • rebase branch in interactive mode

    git rebase -i new_long_feature   
    
  • show branch history

    git show --summary >release.log     
    

Correct commit

  • add to last commit and modify it's description

    git commit --amend
    
  • remove file from staging area

    git reset draw.js
    
  • return to clean working dir (revert local changes)

    git checkout .
    
  • revert local changes in single file

    git checkout engine.js
    
  • revert last commit

    git reset HEAD~
    
  • revert commit in history

    git reset HEAD~1
    
  • revert commit, keep changes in staging

    git reset --soft HEAD~
    
  • revert commit, do not keep changes

    git reset --hard HEAD~
    
  • list base changes

    git reflog
    
  • go back to base version from reflog

    git reset --hard HEAD@{3}
    
  • revert last commit, keep history

    git revert HEAD
    
  • list changes in selected revision

    git show HEAD~1
    
  • compare current version with selected revision (verify revert)

    git diff HEAD~2
    
  • show changes in last commit

    git show HEAD
    
  • compare branches in log

    git log master..new_long_feature
    
  • or

    git log master..origin/master
    

Remote repos

  • check address of remote repo

    git remote -v
    
  • or

    git config --get remote.origin.url
    
  • more verbose

    git remote show origin
    
  • add remote repo to existing working dir

    git remote add origin https://github.com/Biegal/arkanoid-1.git
    
  • add nth remote repo to existing working dir

    git remote add nth-node https://github.com/Biegal/arkanoid-2.git
    
  • rename remote node

    git remote rename nth-node secondary_node
    
  • remove link to remote node

    git remote rm secondary_node
    

Pushing changes to remote repos

  • note: push requires remote repo connected, it can be done via

    git remote add origin {remote-path}
    
  • first push

    git push --set-upstream origin master
    
  • push local changes to remote repo

    git push origin master:master
    
  • subsequent push

    git push
    
  • push from branch

    git checkout new_long_feature
    git push --set-upstream origin new_long_feature:add_scoreboard
    

e.g.

  git push --set-upstream origin local-branch-name:remote-branch-name

next:

  git push secondary
  git push origin my_local:my_remote

Remote repos - other

  • fetch latest changes

    git fetch
    
  • fetch from selected remote host

    git fetch origin
    
  • show log of remote repo

    git log origin/master
    
  • merge latest changes from remote with local repo

    git merge origin/master
    
  • fetch & merge changes

    git pull
    
  • fetch, merge & rebase changes

    git pull --rebase
    

Tags

  • list tags

    git tag
    
  • show changes in tags

    git show v.0.3
    
  • create tag

    git tag v.0.4 2ffb638
    
  • create tag with author, date and description

    git tag -a v.0.6
    
  • list tags with filtering

    git tag -l 'v.0.2*'
    
  • delete tag

    git tag -d v.0.5
    
  • push single tag to remote repo

    git push origin v.0.6
    
  • push all tags

    git push --tags
    

Stash

  • put changes to stash & revert local changes

    git stash
    
  • list changes in stash

    git stash list
    
  • put changes to stash with change description

    git stash save refactoring_of_mouse_move_handler
    
  • show changed files in selected stash

    git stash show stash@{0}
    
  • e.g.

    git stash show <stash-id>
    
  • show changed files with diff in selected stash

    git stash show -p stash@{0}
    
  • apply changes from selected stash

    git stash apply stash@{1}
    
  • apply most recent stash & delete it (requires clean file)

    git stash pop
    
  • delete (drop) selected stash

    git stash drop stash@{1}
    
  • delete all stashes

    git stash clear
    

gitignore

  • edit local excludes (not saved to repo)

    nano .git/info/exclude
    
  • website for generation of gitignore files

    http://gitignore.io/

Aliases

  • define alias "co"

    git config --global alias.co checkout
    
  • use alias "co"

    git co
    
  • define alias "s"

    git config --global alias.s status
    
  • use alias "s"

    git s
    
  • to list defined aliases check config section "alias"

    nano ~/.gitconfig
    
  • define alias to filter log with grep ('!' starts a script)

    git config --global alias.flog '!git log --oneline| grep'
    
  • use grep alias

    git flog paddle
    

Patches

  • create patch file for selected commit (and only this commit: -1)

    git format-patch -1 6cec3f5
    
  • create patch for differences between current and selected branch, save output to given "patches" dir, saves changes that are in current branch but not in given branch

    git format-patch a_big_feature_branch -o patches
    
  • apply changes (& commit) from patch to selected branches

    git checkout a_big_feature_branch
    git am patches/0001-a-nice-change.patch
    
  • or just check for conflicts

    git apply --check ../001-a-nice-change.patch
    
  • or apply without commit

    git apply ../001-a-nice-change.patch   
    

Git Flow Extensions

  • how to install

    https://github.com/nvie/gitflow/wiki/Linux
    
  • how to use git flow extensions:

    https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
    https://danielkummer.github.io/git-flow-cheatsheet/
    

Clean local repo

  • prune local branches

    git remote prune origin
    
  • clean work dir

    git checkout -- .
    git clean -f -d
    

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment