Skip to content

Instantly share code, notes, and snippets.

@ingmarsk
Last active November 10, 2017 09:34
Show Gist options
  • Save ingmarsk/a4c5b620aecc56dfa20a87e1d975383e to your computer and use it in GitHub Desktop.
Save ingmarsk/a4c5b620aecc56dfa20a87e1d975383e to your computer and use it in GitHub Desktop.
Git cheatsheet
GIT
—————————————————————————————————
export PS1='inge$ ' (change command prompt)
Git auto-completion
-----------------------------
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git-completion.bash ~/.git-completion.bash
nano .bash_profile
which git
git --version
git help <command> (press B - Back or F - Forward to navigate)
git config —-global alias.newName commit (set alias for ‘commit’)
alias: co (commit) - st (status) - df (diff) - ad (add) - ps (push) - ck (checkout) - br (branch) - mg (merge)
git add . (adds all files)
git add *.rb (add only ruby files)
git add <filename>
git status
git status -s (short view)
git show (show what changed on that commit)
git log
git log —stat
git log -p <sha> (shows the difference in that commit)
git log -p -1 (show diffs of the first commit)
git log -2 (show last 2 commits)
git log --pretty=format:"%h : %an : %ar : %s"
git log --since=1.weeks
git log —since=“2016-11-10” (can use ‘—before’)
git log —author=“ingmarsk”
git log --grep="pattern"
git gr -5
git log since/until/after="2016-11-20"
git log --since=2.weeks --until=2.days
git config —list
git config —global color.ui true (git colours)
git diff (working dir vs staging)
git diff --staged <file> (staging vs repository)
git diff <sha> <file> (changes on that commit)
git diff <sha1>..<sha2> (diff betwn 2 commits)
git diff <sha1>..<sha2> <file> (diff on that file betwn 2 commits)
git diff --stat --summary <sha-n>..<sha-n>
git rm -f <filename> (removes from working dir & repo)
git mv <oldNameFile> <NewNameFile> (Rename a file)
git mv <fileName> <newDirName/fileName> (Move a file to another dir)
git commit (open default editor to write commit message)
git commit -a -m “” (commit directly to repo without adding to staging area)
git commit —-amend -m “” (Replace last commit with this the changse of this new one, needs to add modified files first)
git checkout <filename> (take file from local-repo & replace it to working copy !!DANGEROUS!!)
git checkout -- <file> (undo-changes to the working dir in the current branch, files added but not commited)
git reset HEAD <filename> (Un-stage file from staging area)
git reset HEAD~1 --soft (Un-commit last commit if it has not been pushed)
git checkout — <checksum> — <file> (get an older version of a file from repo, it will appear as modified on the W.D)
git checkout -- <file> (restore changes from repo to working dir)
git revert <sha1> (reverts to be like it was on that commit. It creates a new commit)
git clean -f (remove untracked files)
USING RESET TO UNDO ANY COMMITS
---------------------------------
git reset -- <option> <sha1> (specify where the HEAD pointer should point to)
-- soft: Just move the HEAD pointer. Does not change the staging indexor W.D. (SAFEST)
-- mixed (default): Move the pointer to the specified commit and change the staging index to match the repository. It does not change the W.D.
-- hard: move the pointer to that commit and change the staging and W.C to match the repo. So any changes that came after that commit will be gone. (DANGER)
REFER TO COMMITS
---------------------
git ls-tree <tree-ish> (list files of that commit)
git ls-tree HEAD
HEAD^ or HEAD~1 (parent commit of the last one)
HEAD^^ or HEAD~2 (grandparent)
b439e14^ (parent of that commit)
git remote add <alias> <url>
git remote (list remote repo)
git remote -v (show remote repo url)
git remote rm <alias>
git remote rename oldName newName
GIT FLOW
-----------
* fetch before start to work (what changes had been made since last time)
* fetch before you push
* fetch often
git fetch <remote> (syncronize local origin/branch with remote branch. But not merge the commits to local branch, to do it its necessary to merge origin/branch to our local branch)
git diff origin/master..master
git merge origin/master
git pull = git fetch + git merge
git pull <Repo> <branch> (git pull origin master)
git push -u <Repo> <branch>
git branch (view current branch)
git branch -v (list branches with its last commit)
git branch -r (list remotes)
git branch -a (list both remote/local)
git branch -a -v
git branch --list *name_to_search* (search branches by pattern)
git branch —-merged
git branch —-no-merged
git branch <new> (creates new branch)
git branch -m <oldName> <newName> (rename branch)
git branch -d <branch> (delete merged branch)
git branch -D <branch> (delete un-merged branch)
git push origin :<branch> (delete branch on GitHub )
git checkout <branch> (switch branch)
git diff <br1>..<br2>
git stash save "messae" (saves W.D changes)
git stash
git stash list
git stash show stash@{0}
git stash show stash@{0} -p
git stash apply (stash out to W.D and keep the stash to the stack)
git stash pop (stash out to W.D and remove the stash to the stack)
git stash drop
git stash clear (remove all stashes)
git checkout -b branch1 (create & switch to a new branch)
git merge —-no-ff branch (Merge with and create a new commit)
git merge --ff-only (Do a merge only if it can do a ff, if not then abort)
git merge --abortgit
sudo rm -R <foldername>
IGNORING FILES
---------------
project/.gitignore
basic regular exp: * ? [aeiou] [0-9]
negate exp: !
ex..
*.php (ignores every file that ends with .php)
!index.php (but don't ingore index.php)
assets/videos/ (ignore all files in the dir)
NEVER REBASE COMMITS THAT HAVE BEEN PUSHED TO A REMOTE REPOSITORY!
Reverting: Undoes whatever changes were made in that commit by making a new commit (but the old one still exists)
Reseting: Undoes changes and actually deletes the commit (DANGEROUS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment