Next step Git for those that already know add and commit
Jakob Christensen Mar 30 Originally published at leruplund.dk on Mar 30, 2018
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.
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.
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 useful 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.
In no specific order whatsoever.
On Windows the Git configuration file is usually placed under “c:\Users[user]”. You can also start an editor from the command prompt.
git config --global -e
To change the default editor for commit messages to Notepad++, add a [core] section to the config file looking like this.
[core] editor = 'C:/put-your-folder-here/Notepad++/notepad++.exe' -multiInst -notabbar
From now on Notepad++ will open when ever you run git commit without the -m switch.
[diff] tool = vsdiffmerge [difftool] prompt = true [difftool "vsdiffmerge"] cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsDiffMerge.exe\" \"$LOCAL\" \"$REMOTE\" //t keepbackup = false trustexistcode = true [merge] tool = vsdiffmerge [mergetool] prompt = true [mergetool "vsdiffmerge"] cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsDiffMerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m keepbackup = false trustexistcode = true
If the repo contains submodules, and you want to bring the code in the submodules down, you’ll need to clone recursively.
git clone --recursive https://github.com/hocuspocus/icsharp.git
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.
[submodule "Engine"] path = Engine url = https://github.com/scriptcs/scriptcs.git
After saving .gitsubmodule, run the command.
git submodule sync
It seems that this may detach from HEAD, so a checkout may be necessary (before making any local changes).
If you have trouble downloading the code for the submodule, try running the command:
git submodule update --remote
If there is a merge tool, you can start your merge tool (set in the config file).
Start by fetching all from the remote repo:
git fetch origin
Then compare with local:
git log HEAD..origin/master --oneline
If you are happy with the results, you may merge the remote changes with the local repo:
Show remote URL for “origin”:
git remote get-url origin
For a bit more information you may use:
git remote show origin
I your remote has moved, you can change the URL using set-url:
git remote set-url origin https://email@example.com/myteam/myproject.git
Delete the remote branch:
git push -d <remote_name> <branch_name>
git push -d origin my-feature-branch
You may also use:
git push <remote_name> :<branch_name>
Delete the local branch:
git branch -d <branch_name>
Undo all unstaged local changes:
git checkout .
Undo git add for at single file:
git reset folder/file.cs
git add . :
git reset .
git rm . -r --cached git add . git commit -m "Fixed untracked files"
If you are tired of typing long hard-to-forget commands you can create aliases.
git config --global alias.a "add ." git config --global alias.c "commit"
You can now just type
git a to add unstaged files.
Aliases can also be added directly to the config file.
[alias] a = add . c = commit