To delete a branch
Local branch:git branch -d <local_branch>
Remote branch: git push origin --delete <remote_branch>To create new branch from existing branch
git checkout -b <new_branch_name> <source_branch_name>
To push my branch to remote server
git push -u origin <my_branch>
To view commit history logs
git reflog
To reset to particular commit log
git reset HEAD@{N}
*N is target number of commit log to reset to.To show last git commit message on current checkout branch
git show --summary
To revert commits in certain branch to particular commit
git checkout <branch>
git reset --hard <commit-hash>
git push -f origin <branch>
To fetch all git branches
git pull --all
To create local branch to tracks remote branch
git checkout --track origin/john_branch
where --track is shorthand for git checkout -b [branch] [remotename]/[branch]To reset local branch to match latest remote branch
git fetch origin
git reset --hard origin/master
git clean -fd
To rename a branch
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
Successful git branching
Ref: https://nvie.com/posts/a-successful-git-branching-model/Creating a feature branch
git checkout -b myfeature develop
Incorporating a finished feature on develop
git checkout develop
git merge --no-ff myfeature
.
.
.
git branch -d myfeature
git push origin develop
Using Git
Global Settings
Related Setup: https://gist.github.com/hofmannsven/6814278Related Pro Tips: https://ochronus.com/git-tips-from-the-trenches/
Interactive Beginners Tutorial: http://try.github.io/
Reminder
Pressminus + shift + s and return to chop/fold long lines!Show folder content:
ls -laNotes
Do not put (external) dependencies in version control!Setup
See where Git is located:which gitGet the version of Git:
git --versionCreate an alias (shortcut) for
git status:
git config --global alias.st statusHelp
Help:git helpGeneral
Initialize Git:git initGet everything ready to commit:
git add .Get custom file ready to commit:
git add index.htmlCommit changes:
git commit -m "Message"Add and commit in one step:
git commit -am "Message"Remove files from Git:
git rm index.htmlUpdate all changes:
git add -uRemove file but do not track anymore:
git rm --cached index.htmlMove or rename files:
git mv index.html dir/index_new.htmlUndo modifications (restore files from latest commited version):
git checkout -- index.htmlRestore file from a custom commit (in current branch):
git checkout 6eb715d -- index.htmlReset
Go back to commit:git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6Soft reset (move HEAD only; neither staging nor working dir is changed):
git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6Undo latest commit:
git reset --soft HEAD~ Mixed reset (move HEAD and change staging to match repo; does not affect working dir):
git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6Hard reset (move HEAD and change staging dir and working dir to match repo):
git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6Update & Delete
Test-Delete untracked files:git clean -nDelete untracked files (not staging):
git clean -fUnstage (undo adds):
git reset HEAD index.htmlCommit to most recent commit:
git commit --amend -m "Message"Update most recent commit message:
git commit --amend -m "New Message"Branch
Show branches:git branchCreate branch:
git branch branchnameChange to branch:
git checkout branchnameCreate and change to new branch:
git checkout -b branchnameRename branch:
git branch -m branchname new_branchname or:
git branch --move branchname new_branchnameShow all completely merged branches with current branch:
git branch --mergedDelete merged branch (only possible if not HEAD):
git branch -d branchname or:
git branch --delete branchnameDelete not merged branch:
git branch -D branch_to_deleteMerge
True merge (fast forward):git merge branchnameMerge to master (only if fast forward):
git merge --ff-only branchnameMerge to master (force a new commit):
git merge --no-ff branchnameStop merge (in case of conflicts):
git merge --abortStop merge (in case of conflicts):
git reset --merge // prior to v1.7.4Merge only one specific commit:
git cherry-pick 073791e7Stash
Put in stash:git stash save "Message"Show stash:
git stash listShow 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 clearGitignore & Gitkeep
About: https://help.github.com/articles/ignoring-filesUseful templates: https://github.com/github/gitignore
Add or edit gitignore:
nano .gitignoreTrack empty dir:
touch dir/.gitkeepLog
Show commits:git logShow oneline-summary of commits:
git log --onelineShow oneline-summary of commits with full SHA-1:
git log --format=onelineShow oneline-summary of the last three commits:
git log --oneline -3Show only custom commits:
git log --author="Sven"
git log --grep="Message"
git log --until=2013-01-01
git log --since=2013-01-01Show only custom data of commit:
git log --format=short
git log --format=full
git log --format=fuller
git log --format=email
git log --format=rawShow changes:
git log -pShow every commit since special commit for custom file only:
git log 6eb715d.. index.htmlShow changes of every commit since special commit for custom file only:
git log -p 6eb715d.. index.htmlShow stats and summary of commits:
git log --stat --summaryShow history of commits as graph:
git log --graphShow history of commits as graph-summary:
git log --oneline --graph --all --decorateCompare
Compare modified files:git diffCompare modified files and highlight changes only:
git diff --color-words index.htmlCompare modified files within the staging area:
git diff --stagedCompare branches:
git diff master..branchnameCompare branches like above:
git diff --color-words master..branchname^Compare commits:
git diff 6eb715d
git diff 6eb715d..HEAD
git diff 6eb715d..537a09fCompare commits of file:
git diff 6eb715d index.html
git diff 6eb715d..537a09f index.htmlCompare without caring about spaces:
git diff -b 6eb715d..HEAD or:
git diff --ignore-space-change 6eb715d..HEADCompare without caring about all spaces:
git diff -w 6eb715d..HEAD or:
git diff --ignore-all-space 6eb715d..HEADUseful comparings:
git diff --stat --summary 6eb715d..HEADBlame:
git blame -L10,+1 index.htmlReleases & Version Tags
Show all released versions:git tagShow all released versions with comments:
git tag -l -n1Create release version:
git tag v1.0.0Create release version with comment:
git tag -a v1.0.0 -m 'Message'Checkout a specific release version:
git checkout v1.0.0Collaborate
Show remote:git remoteShow remote details:
git remote -vAdd remote origin from GitHub project:
git remote add origin https://github.com/user/project.gitAdd remote origin from existing empty project on server:
git remote add origin ssh://root@123.123.123.123/path/to/repository/.gitRemove origin:
git remote rm originShow remote branches:
git branch -rShow all branches:
git branch -aCompare:
git diff origin/master..masterPush (set default with
-u):
git push -u origin masterPush to default:
git push origin masterFetch:
git fetch originFetch a custom branch:
git fetch origin branchname:local_branchnamePull:
git pullPull specific branch:
git pull origin branchnameMerge fetched commits:
git merge origin/masterClone to localhost:
git clone https://github.com/user/project.git or:
git clone ssh://user@domain.com/~/dir/.gitClone to localhost folder:
git clone https://github.com/user/project.git ~/dir/folderClone specific branch to localhost:
git clone -b branchname https://github.com/user/project.gitDelete remote branch (push nothing):
git push origin :branchname or:
git push origin --delete branchnameArchive
Create a zip-archive:git archive --format zip --output filename.zip masterExport/write custom log to a file:
git log --author=sven --all > log.txtTroubleshooting
Ignore files that have already been committed to a Git repository: http://stackoverflow.com/a/1139797/1815847Security
Hide Git on the web via.htaccess: RedirectMatch 404 /\.git
(more info here: http://stackoverflow.com/a/17916515/1815847)Large File Storage
Website: https://git-lfs.github.com/Install:
brew install git-lfsTrack
*.psd files: git lfs track "*.psd" (init, add, commit and push as written above)