# Delete remote branchgitpushorigin-dcleanup
# Delete local branchgitbranch-Dcleanup
Rename branches
# Rename local and remote branch# Example: rename master to old-mastergitcheckoutmastergitbranch-mold-mastergitpushorigin:masterold-mastergitpushorigin-uold-master
Fix 'is unmerged' error
# ton@ubuntu:~/work/git/test-repo$ git checkout -- .# error: path 'Dockerfile' is unmerged# error: path 'Makefile' is unmergedgitresetMakefile
gitresetDockerfile
Clear all staged files
gitrestore--staged.
Discard local changes for specific file
gitcheckoutpath/to/file.cpp
# This can resolve for example an issue when doing "git pull origin master" to # feature branch: "Please commit your changes or stash them before you merge. ( Note: you will lose your changes )"
Prevent LF -> CLRF ( line breaks ) change on one repo
gitconfig--localcore.autocrlffalse
Undo a merge before pushing
1.Checkthecommithashwithgitreflog
#Example:#PS C:\work\git\example-branch> git reflog#576e60f (HEAD -> master) HEAD@{1}: checkout: moving from feature-usb-support to master#4fa9e62 (origin/master, origin/HEAD) HEAD@{4}: checkout: moving from feature-usb-led-support to master#ca1499b (origin/feature-usb-led-support, feature-usb-led-support) HEAD@{5}: pull: Fast-forward#b1f3ab7 HEAD@{6}: checkout: moving from feature-usb-support to feature-usb-led-support#ead3f6e (origin/feature-usb-support, feature-usb-support) HEAD@{7}: checkout: moving from feature-usb-led-support to feature-usb-support#b1f3ab7 HEAD@{8}: pull: Fast-forward#fc2dced HEAD@{9}: checkout: moving from feature-usb-support to feature-usb-led-support#ead3f6e (origin/feature-usb-support, feature-usb-support) HEAD@{10}: commit: Set LED on when communicating with devices#83e438d HEAD@{11}: commit (merge): Merge LED handling changes.#4fa9e62 (origin/master, origin/HEAD) HEAD@{13}: checkout: moving from feature-usb-led-support to master#fc2dced HEAD@{14}: checkout: moving from feature-usb-support to feature-usb-led-supportgitreset--hard4fa9e62
Fix 'Failed to push some refs to..'
gitpush-uoriginbranch-name-here
# Some git providers throw this error for example when trying to push a new branch
Undo the last commit before pushing
# To undo and keep the changed made to files:gitresetHEAD^
# more? Error: Add " around HEAD^Error:gitreset"HEAD^"# To undo and remove changes made to files:gitreset--hardHEAD^
# To remove last two commits and file changes:gitreset--hardHEAD~2
Discard untracked files
# List all:gitclean-n
# Delete ( directories and files ):gitclean-fd
Delete remote file ( keep locally )
gitrm--cached<filename>
gitrm--cached-r<dir_name>
gitcommit-m"Removed folder from repository"gitpushoriginmaster
# Example: conflict in merging feature branch 'io-sampler-optimizing' to 'develop' branch1.gitcheckoutdevelop
2.gitpull
3.gitcheckoutio-sampler-optimizing
4.gitpullorigindevelop# this creates the conflict lines in local files5.Fixtheconflicts
6.gitadd.
7.gitcommit-m"Resolved merge conflicts."8.gitpushoriginio-sampler-optimizing
# Some other tutorial:https://confluence.atlassian.com/bitbucket/resolve-merge-conflicts-704414003.html
Change branch name
# Rename your local branch.# If you are on the branch you want to rename:gitbranch-mnew-name
# If you are on a different branch:gitbranch-mold-namenew-name
# Delete the old-name remote branch and push the new-name local branch.gitpushorigin:old-namenew-name
# Reset the upstream branch for the new-name local branch.# Switch to the branch and then:gitpushorigin-unew-name
Revert to specific commit
gitreverta66b89f
Delete last pushed commit
gitpush-foriginlast_known_good_commit:branch_name
Start SSH agent on Linux
eval$(ssh-agent)ssh-add~/.ssh/github/github-key
Fix issue where git keeps asking for user credentials
# Change this:ton@mx-5:~/beaglebone-os$gitconfigremote.origin.url
https://github.com/protoni/beaglebone-os
# To this:gitconfigremote.origin.url"git@github.com:protoni/beaglebone-os"
Fix issue with Changes not staged for commit
# I running this:gitcheckout--.
# Results in this:Encountered28filesthatshouldhavebeenpointers,butweren't:'# Try this:gitrm--cached-r.
gitreset--hard
gitrm.gitattributes
gitreset.
gitcheckout.
Emojis in commits
# Github commits support emojis, for example :wrench: is converted to:๐ง
# Example:gitcommit-m":wrench: Fixed a bug"# Here are some more emojis# Just add the ':' to start and end๐(rocket)-Deployingorreleasingnewfeatures
๐(bug)-Fixingabug
โจ(sparkles)-Introducingnewfeatures
๐งน(broom)-Refactoringorcleaningupcode
๐ฆ(package)-Updatingoraddingdependencies
๐(memo)-Updatingdocumentation
๐ง(construction)-Workinprogress
๐ง(wrench)-Changingconfigurationfilesortooling
๐ (hammerandwrench)-Makingchangestothebuildprocessortooling
โฌ๏ธ(arrowup)-Upgradingdependencies
โฌ๏ธ(arrowdown)-Downgradingdependencies
๐(horse)-Improvingperformance
๐จ(art)-ImprovingUI/CSSstyling
๐(fireengine)-Addressingacriticalissueorhotfix
โ (white_check_mark)-Addingtests
๐(lock)-Fixingsecurityissuesorimprovingsecurity
๐(green_heart)-FixingCI/CDbuild
๐(lipstick)-ImprovingUI/CSSstyling
๐(globe_with_meridians)-Internationalizationandlocalization
โ(heavy_minus_sign)-Removingcodeorfiles
โ(heavy_plus_sign)-Addingcodeorfiles
๐(tada)-Initialcommitormajorrelease
๐ก(bulb)-Addingcommentsordocumentingsourcecode
๐ง(penguin)-FixingsomethingonLinux
๐(green_apple)-FixingsomethingonmacOS
๐(checkered_flag)-FixingsomethingonWindows
๐ฅ(bustsinsilhouette)-Addingcontributor(s)๐(bookmark)-Releasing/versioningtags
๐จ(rotating_light)-Fixingcompiler/linterwarnings