When I first started using Git a while back it seemed pretty straight forward although I had to get used to the disconnected nature of Git which is different than how TFS works (or used to work). I mostly used Visual Studio’s Git integration and usually it worked like a charm.<\/p>\n
But then after a while things started to get complicated. I had to work with submodules, change the remote URL, and handle untracked files. That is when I decided to move out of my comfort zone in Visual Studio and into the Git CLI.<\/p>\n
The Git CLI is not easily remembered and everything can be done in more than one way so I started my own compilation of useful commands. Below is the result of that compilation. I hope it can be hopeful for those of you going through the same process and please let me know if you have any Git gems of your own that belong on the list.<\/p>\n
In no specific order whatsoever.<\/p>\n
On Windows the Git configuration file is usually placed under “c:\\Users[user]”. You can also start an editor from the command prompt.<\/p>\n
git config --global -e\n<\/code><\/pre>\nSet editor for commit messages<\/h3>\n
To change the default editor for commit messages to Notepad++, add a [core] section to the config file looking like this.<\/p>\n
[core]\n editor = 'C:\/put-your-folder-here\/Notepad++\/notepad++.exe' -multiInst -notabbar\n<\/code><\/pre>\nFrom now on Notepad++ will open when ever you run git commit without the -m switch.<\/p>\n
Set merge tool to Visual Studio<\/h3>\n[diff]\n tool = vsdiffmerge\n[difftool]\n prompt = true\n[difftool \"vsdiffmerge\"]\n cmd = \\\"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\Professional\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\TeamFoundation\\\\Team Explorer\\\\vsDiffMerge.exe\\\" \\\"$LOCAL\\\" \\\"$REMOTE\\\" \/\/t\n keepbackup = false\n trustexistcode = true\n[merge]\n tool = vsdiffmerge\n[mergetool]\n prompt = true\n[mergetool \"vsdiffmerge\"]\n cmd = \\\"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\Professional\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\TeamFoundation\\\\Team Explorer\\\\vsDiffMerge.exe\\\" \\\"$REMOTE\\\" \\\"$LOCAL\\\" \\\"$BASE\\\" \\\"$MERGED\\\" \/\/m\n keepbackup = false\n trustexistcode = true\n<\/code><\/pre>\nSubmodules<\/h2>\nCloning submodules<\/h3>\n
If the repo contains submodules, and you want to bring the code in the submodules down, you’ll need to clone recursively.<\/p>\n
git clone --recursive https:\/\/github.com\/hocuspocus\/icsharp.git\n<\/code><\/pre>\nChange submodule to own fork<\/h3>\n
If you have cloned a repo with a submodule and you want to change the submodule to a different fork (if for example you have forked the submodule), you need to edit the URL in the file .gitsubmodule.<\/p>\n
[submodule \"Engine\"]\n path = Engine\n url = https:\/\/github.com\/scriptcs\/scriptcs.git\n<\/code><\/pre>\nAfter saving .gitsubmodule, run the command.<\/p>\n
git submodule sync\n<\/code><\/pre>\nIt seems that this may detach from HEAD, so a checkout may be necessary (before making any local changes).<\/p>\n
git checkout\n<\/code><\/pre>\nIf you have trouble downloading the code for the submodule, try running the command:<\/p>\n
git submodule update --remote\n<\/code><\/pre>\nStart merge tool<\/h2>\n
If there is a merge tool, you can start your merge tool (set in the config file).<\/p>\n
git mergetool\n<\/code><\/pre>\nCompare to remote<\/h2>\n
Start by fetching all from the remote repo:<\/p>\n
git fetch origin\n<\/code><\/pre>\nThen compare with local:<\/p>\n
git log HEAD..origin\/master --oneline\n<\/code><\/pre>\nIf you are happy with the results, you may merge the remote changes with the local repo:<\/p>\n
git merge\n<\/code><\/pre>\nShow remote URL<\/h2>\n
Show remote URL for “origin”:<\/p>\n
git remote get-url origin\n<\/code><\/pre>\nFor a bit more information you may use:<\/p>\n
git remote show origin\n<\/code><\/pre>\nI your remote has moved, you can change the URL using set-url<\/em>:<\/p>\ngit remote set-url origin https:\/\/hocuspocus@bitbucket.org\/myteam\/myproject.git\n<\/code><\/pre>\nDelete branch<\/h2>\n
Delete the remote branch:<\/p>\n
git push -d \n<\/code><\/pre>\nFor example:<\/p>\n
git push -d origin my-feature-branch\n<\/code><\/pre>\nYou may also use:<\/p>\n
git push :\n<\/code><\/pre>\nDelete the local branch:<\/p>\n
git branch -d \n<\/code><\/pre>\nDelete local changes<\/h2>\n
Undo all unstaged local changes:<\/p>\n
git checkout .\n<\/code><\/pre>\nUndo git add for at single file:<\/p>\n
git reset folder\/file.cs\n<\/code><\/pre>\nUndo git add .<\/code> :<\/p>\ngit reset .\n<\/code><\/pre>\nFix untracked files<\/h2>\ngit rm . -r --cached\ngit add .\ngit commit -m \"Fixed untracked files\"\n<\/code><\/pre>\nCreate an alias for a command<\/h2>\n
If you are tired of typing long hard-to-forget commands you can create aliases.<\/p>\n
git config --global alias.a \"add .\"\ngit config --global alias.c \"commit\"\n<\/code><\/pre>\nYou can now just type git a<\/code> to add unstaged files.<\/p>\nAliases can also be added directly to the config file.<\/p>\n
[alias]\n a = add .\n c = commit\n<\/code><\/pre>\nShare this:<\/h3>