Skip to content

Instantly share code, notes, and snippets.

@adeleinr
Forked from hofmannsven/README.md
Last active February 19, 2019 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adeleinr/70ec9cd3975c08da4691 to your computer and use it in GitHub Desktop.
Save adeleinr/70ec9cd3975c08da4691 to your computer and use it in GitHub Desktop.
Using Git
===============
Global Settings
-----------
Setup autocomplete tools: brew install git bash-completion
Then add this to ~/.bash_profile :
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
Related Setup: https://gist.github.com/hofmannsven/6814278
Related Pro Tips: https://ochronus.com/git-tips-from-the-trenches/
Interactive Beginners Tutorial: http://try.github.io/
Branch
-----------
Show branches:
`git branch`
Create branch:
`git branch branchname`
Change to branch:
`git checkout branchname`
Create and change to new branch:
`git checkout -b branchname`
Rename branch:
`git branch -m branchname new_branchname` or:
`git branch --move branchname new_branchname`
Show all completely merged branches with current branch:
`git branch --merged`
Delete merged branch (only possible if not HEAD):
`git branch -d branchname` or:
`git branch --delete branchname`
Delete not merged branch:
`git branch -D branch_to_delete`
Rebase when Collaborating
------------------------
When working in a team and want to merge to main:
git add .
git commit -a -m "Your msg"
git push // This pushes to your branch
git checkout dev
git pull // pulls latest changes in master (dev)
// Brings changes from your branch to local dev. You need to resolve conflicts if any
// An editor will pop up, then you put an 'f' in front of all the commits you want
// to keep but not show its msg in the history, and keep only one commit with the most relevant
// commit msg
git rebase -i dev
git push // pushes to master
Undo (anything, including a undoing a rebase)
---------------------------------------------
// The reflog contains information about the old state of branches and
// allows you to go back to that state if necessary.
git reflog
// Select the commit you want to go back to
git reset --hard HEAD@{5}
Go back to commit:
`git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6`
Soft reset (move HEAD only; neither staging nor working dir is changed):
`git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6`
Mixed reset (move HEAD and change staging to match repo; does not affect working dir):
`git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6`
Hard reset (move HEAD and change staging dir and working dir to match repo):
`git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6`
Revert to an old version of a file
If you messed up in "abbcdf" and want the version right before "abbcdf", you can do git checkout "abbcdf~1" path/to/file.
Setup
-----------
See where Git is located:
`which git`
Get the version of Git:
`git --version`
Create an alias (shortcut) for `git status`:
`git config --global alias.st status`
Help
-----------
Help:
`git help`
General
-----------
Initialize Git:
`git init`
Get verything ready to commit:
`git add .`
Get custom file ready to commit:
`git add index.html`
Commit changes:
`git commit -m "Message"`
Add and commit in one step:
`git commit -am "Message"`
Remove files from Git:
`git rm index.html`
Remove all .git files to start fresh
rm -rf .git
Unversion all files (Untrack but not remove)
All files in a dir:
git ls-files | xargs git rm --cached
git rm -r --cached .
A single file:
git rm -r --cached WEB-INF
Update all changes:
`git add -u`
Kepp file but do not track anymore:
`git rm --cached index.html`
Move or rename files:
`git mv index.html dir/index_new.html`
Undo modifications (restore files from latest commited version):
`git checkout -- index.html`
Restore file from a custom commit (in current branch):
`git checkout 6eb715d -- index.html`
Update & Delete
-----------
Test-Delete untracked files:
`git clean -n`
Delete untracked files (not staging):
`git clean -f`
Unstage (undo adds):
`git reset HEAD index.html`
Commit to most recent commit:
`git commit --amend -m "Message"`
Update most recent commit message:
`git commit --amend -m "New Message"`
Rollback
`git log`
`git reset --hard <tag/branch/commit id>`
Merge
-----------
True merge (fast forward):
`git merge branchname`
Merge to master (only if fast forward):
`git merge --ff-only branchname`
Merge to master (forc a new commit):
`git merge --no-ff branchname`
Stop merge (in case of conflicts):
`git merge --abort`
Stop merge (in case of conflicts):
`git reset --merge` // prior to v1.7.4
Stash
-----------
Put in stash:
`git stash save "Message"`
Show stash:
`git stash list`
Show stash stats:
`git stash show stash@{0}`
Show stash changes:
`git stash show -p stash@{0}`
Use custom stash item and drop it:
`git stash pop stash@{0}`
Use custom stash item and do not drop it:
`git stash apply stash@{0}`
Delete custom stash item:
`git stash drop stash@{0}`
Delete complete stash:
`git stash clear`
Gitignore & Gitkeep
-----------
About: https://help.github.com/articles/ignoring-files
Useful templates: https://github.com/github/gitignore
Add or edit gitignore:
`nano .gitignore`
Track empty dir:
`touch dir/.gitkeep`
Log
-----------
Show commits:
`git log`
Show oneline-summary of commits:
`git log --oneline`
Show oneline-summary of commits with full SHA-1:
`git log --format=oneline`
Show oneline-summary of the last three commits:
`git log --oneline -3`
Show only custom commits:
`git log --author="Sven"`
`git log --grep="Message"`
`git log --until=2013-01-01`
`git log --since=2013-01-01`
Show only custom data of commit:
`git log --format=short`
`git log --format=full`
`git log --format=fuller`
`git log --format=email`
`git log --format=raw`
Show changes:
`git log -p`
Show every commit since special commit for custom file only:
`git log 6eb715d.. index.html`
Show changes of every commit since special commit for custom file only:
`git log -p 6eb715d.. index.html`
Show stats and summary of commits:
`git log --stat --summary`
Show history of commits as graph:
`git log --graph`
Show history of commits as graph-summary:
`git log --oneline --graph --all --decorate`
Compare
-----------
Compare modified files:
`git diff`
Compare modified files and highlight changes only:
`git diff --color-words index.html`
Compare modified files within the staging area:
`git diff --staged`
Compare branches:
`git diff master..branchname`
Compare branches like above:
`git diff --color-words master..branchname^`
Compare commits:
`git diff 6eb715d`
`git diff 6eb715d..HEAD`
`git diff 6eb715d..537a09f`
Compare commits of file:
`git diff 6eb715d index.html`
`git diff 6eb715d..537a09f index.html`
Compare without caring about spaces:
`git diff -b 6eb715d..HEAD` or:
`git diff --ignore-space-change 6eb715d..HEAD`
Compare without caring about all spaces:
`git diff -w 6eb715d..HEAD` or:
`git diff --ignore-all-space 6eb715d..HEAD`
Useful comparings:
`git diff --stat --summary 6eb715d..HEAD`
Releases & Version Tags
-----------
Show all released versions:
`git tag`
Show all released versions with comments:
`git tag -l -n1`
Create release version:
`git tag v1.0`
Create release version with comment:
`git tag -a v1.0 -m 'Message'`
Checkout a specific release version:
`git checkout v1.0`
Tag using a comit hash id
`git tag -a v1.2 9fceb02`
Collaborate
-----------
Show remote:
`git remote`
Show remote details:
`git remote -v`
Add remote origin from GitHub:
`git remote add origin https://github.com/user/project.git`
Remove origin:
`git remote rm origin`
Show remote branches:
`git branch -r`
Show all branches:
`git branch -a`
Compare:
`git diff origin/master..master`
Push (set default with `-u`):
`git push -u origin master`
Push to default:
`git push origin master`
Fetch:
`git fetch origin`
Pull:
`git pull`
Pull specific branch:
`git pull origin branchname`
Merge fetched commits:
`git merge origin/master`
Clone to localhost:
`git clone https://github.com/user/project.git` or:
`git clone ssh://user@domain.com/~/dir/.git`
Clone to localhost folder:
`git clone https://github.com/user/project.git ~/dir/folder`
Clone specific branch to localhost:
`git clone -b branchname https://github.com/user/project.git`
Delete remote branch (push nothing):
`git push origin :branchname` or:
`git push origin --delete branchname`
If you checkout head in order to rollback, you end up without a branch, so do this in order to be able to get a branch and push your changes to origin:
Create a branch where you are, then switch to master and merge it (What to do with commit made in a detached head):
git branch my-temporary-work
git checkout master
git merge my-temporary-work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment