Skip to content

Instantly share code, notes, and snippets.

@IngmarBoddington
Last active September 15, 2022 13:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngmarBoddington/4205760 to your computer and use it in GitHub Desktop.
Save IngmarBoddington/4205760 to your computer and use it in GitHub Desktop.
Git Reference
git add . [-A]
- Recursively add all modified files to staging area
-A - Include removed files
git blame
- Show last modigified timestamp / author for each line of a file
git branch [-a] [-r] [-m] [-d] [-D] [<branch-name>]
- Lists local branches, give name to create branch
- Flags:git
-a - Include remote branches
-r - Remote branches
-m - Rename current branch (with branch name)
-d - Delete branch (with branch name), prevents from deleting unmerged changes
-D - Delete branch (with branch name)
git checkout [-b] <branch> [<remote/branch>]
- Switch the current working directory to the specified branch
-b - Create local branch
<remote/branch> - Specify remote branch to create local branch from (with -b)
git checkout <commit> [<file>]
- Update all working files in directory (or specified file only) to commit
- Use HEAD as commit to get back to current version
git checkout <file>
- Rollback uncommited changes in file
git clean -f [<path>]
- Remove untracked files, optionally limit to path
git clean -df
- Remote untracked files and directories
git clean -xf
- Remove untracked files and ignored files (as specified by .gitignore)
git commit -m "<message>"
- Commit staged files with commit message
git commit --amend [-m "<message>"|--no-edit]
- Amend and replace commit with staged changes, optionally update message
git config --global user.email "<email>"
git config --global user.name "<name>"
- Set email and name for commits
git config --global diff.tool vi
git config --global merge.tool vi
git config --global --add difftool.prompt false
- Set diff tool and prevent difftool prompt
git diff
- check diff of all unstaged changes
git fetch <remote> [<branch>]
- Fetch all branches from remote, or optionally specify branch
git init
- Initialize a new git repository in current directory
git init --bare
- Initialize a new git repository in current directory, with no central repo (for git servers)
git log [<since>..<until>] [<file>] [-n <limit>] [-p]
- Show commit history
<since>..<until> - to show hitory between commits or since branching
e.g. git log master feature
<file> - limit to commits including file
-n <limit> - optional limit
-p - full patch info
- Flags:
--oneline - Reduce to single line per commit
--stat - Add file changes with line counts
--author="<pattern>" - Show commits by author (string / regex)
--grep="<pattern>" - Show commits by searching message (string /
regex)
--graph - Display simple graph
git ls-remote [<repo>]
- Display remote repository references
git merge [--no-ff] <[remote/]branch>
- Merge branches into current branch, no-ff to always merge commit
git merge -X rename-threshold=<percent> branch
- Merge branch into current working directory allowing for file renaming
git merge --abort
- rollback merge in case of conflicts between branches (to last commit)
git mergetool
- Launch mergetool for merge conflict resolution
git pull <remote> [<branch>] [--rebase]
- Fetch and merge in a single action, optionally specify branch, optionally specify rebase rather than merge
git push <remote> <branch>|-all
- Push changes to remote repo
git push <remote> --tags
- Push tags to remote repo
git push <remote> :<branch>
- Delete a remote branch
git rebase [-i] <base>
- Rebase current branch with specified reference (merges history)
- For combining multiple commits into one
- Flags:
-i - interactive
git reflog [--relative-date]
- Show HEAD history, optionally show relative date
git remote [-v]
- Displays remotes, -v for all uni-directional remote repos deails (fetch / push for each)
git remote rename <old-name> <new-name>
- Rename remote
git remote add <name> <url>
- Add a remote repository for current project
git remote show
- Show all current remote repos for current working directory
git remote rm <name>
- Remove a remote branch from local
git reset [<file>]
- Unstage all or unspecified file
git reset [--hard] <commit>
- Unstage changes, use HEAD / '' for current snapshot
- Flags:
--hard - Reset files in working directory to commit
git restore <file>
- Return file to current commit
git restore --staged <file>
- Unstage file
git revert [-m1] <commit>
- Revert a commit, use HEAD to revert last commit
-m1 - Revert merge commit (to mainline parent)
git stash [-u]
- Stash (local store without commit) current changes on branch
- Add -u to include untracked (new) files
git stash pop
- Bring back stashed changes
-----
HEAD is latest commit, can use HEAD~n to specify a number of commits ago
-----
rm .git/index
- Undo staged files (when there is no HEAD yet)
git rev-list --ancestry-path $(git merge-base <common-ancestor-branch> <branch>)..<branch> | tail -1
- Find first commit on branch since fork
git --no-pager show -s --format='%an <%ae>' <commit_id>
- Show author of a commit
WORKFLOWS
Pull Requests - Fork, Clone, Branch, Push, Pull Request
1. Fork the repo (if not done already) - In GitHub UI
2. Clone the fork locally (if not done already) - git clone <repo>
3. Setup the upstream locally, i.e. the original repo (if not done already) - git remote add --track <branch> upstream <repo> && git fetch upstream
4. Create a branch locally - git checkout -b <branch>
5. Make changes
6. Commit changes and push to fork - git add . && git commit -m "<message>" && git push origin <branch>
7. Raise a pull request in the original repo - in GitHub UI
Post Pull Request Merge
1. Cleanup the branch on the fork - git checkout <branch(main/master)> && git branch -d <branch> && git push origin :<branch>
2. Pull latest upstream (which now includes the branch) - git pull upstream <branch(main/master)>
3. Push latest to fork (so we work on latest) - git push origin <branch(main/master)>
Remove committed files which are subsequently added to .gitignore
1. git rm -r --cached .
2. git add .
3. git commit -m "<message>"
4. etc
SUPER COMMANDS!
PRETTY PROJET HISTORY (credit www.kerrybuckley.org)
git log --pretty=%h | tail -r | while read sha ; do if [[ -z $(git rev-parse --verify $sha^2 2> /dev/null) ]] ; then echo '----' ; git show --summary $sha | tail -n+5 | egrep -v '^ (?:create|delete) mode' ; git --no-pager diff --color -U999 $sha~ $sha | awk '/diff --git/ { sub("^a/", "", $3) ; print "===" $3 } /diff --git/, /@@/ { next } { print }' ; fi ; done | ansifilter -H | sed 's/^----$/<hr \>>/;s/===\(.*\)/<h2>\1<\/h2>/' > history.html
SETUP GLOBAL GIT IGNORE
touch ~/.gitignore
git config --global core.excludesFile ~/.gitignore
//Update ignores in file
//Remove files added to gitignore since commit
git rm -r --cached .
git add .
git commit -m ".gitignore fix"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment