Warning: Declaration of Jetpack_IXR_Client::query() should be compatible with IXR_Client::query(...$args) in /var/www/leruplund.dk/public_html/wp-content/plugins/jetpack/class.jetpack-ixr-client.php on line 30

Warning: Cannot modify header information - headers already sent by (output started at /var/www/leruplund.dk/public_html/wp-content/plugins/jetpack/class.jetpack-ixr-client.php:0) in /var/www/leruplund.dk/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1775
{"id":1012,"date":"2018-03-30T08:35:52","date_gmt":"2018-03-30T08:35:52","guid":{"rendered":"http:\/\/leruplund.dk\/?p=1012"},"modified":"2018-03-30T08:37:42","modified_gmt":"2018-03-30T08:37:42","slug":"next-step-git-for-those-that-already-know-add-and-commit","status":"publish","type":"post","link":"http:\/\/leruplund.dk\/2018\/03\/30\/next-step-git-for-those-that-already-know-add-and-commit\/","title":{"rendered":"Next step Git for those that already know add and commit"},"content":{"rendered":"

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

The list<\/h1>\n

In no specific order whatsoever.<\/p>\n

Edit configuration<\/h2>\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>\n

Set 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>\n

From 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>\n

Submodules<\/h2>\n

Cloning 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>\n

Change 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>\n

After saving .gitsubmodule, run the command.<\/p>\n

git submodule sync\n<\/code><\/pre>\n

It 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>\n

If you have trouble downloading the code for the submodule, try running the command:<\/p>\n

git submodule update --remote\n<\/code><\/pre>\n

Start 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>\n

Compare to remote<\/h2>\n

Start by fetching all from the remote repo:<\/p>\n

git fetch origin\n<\/code><\/pre>\n

Then compare with local:<\/p>\n

git log HEAD..origin\/master --oneline\n<\/code><\/pre>\n

If you are happy with the results, you may merge the remote changes with the local repo:<\/p>\n

git merge\n<\/code><\/pre>\n

Show remote URL<\/h2>\n

Show remote URL for “origin”:<\/p>\n

git remote get-url origin\n<\/code><\/pre>\n

For a bit more information you may use:<\/p>\n

git remote show origin\n<\/code><\/pre>\n

I your remote has moved, you can change the URL using set-url<\/em>:<\/p>\n

git remote set-url origin https:\/\/hocuspocus@bitbucket.org\/myteam\/myproject.git\n<\/code><\/pre>\n

Delete branch<\/h2>\n

Delete the remote branch:<\/p>\n

git push -d  \n<\/code><\/pre>\n

For example:<\/p>\n

git push -d origin my-feature-branch\n<\/code><\/pre>\n

You may also use:<\/p>\n

git push  :\n<\/code><\/pre>\n

Delete the local branch:<\/p>\n

git branch -d \n<\/code><\/pre>\n

Delete local changes<\/h2>\n

Undo all unstaged local changes:<\/p>\n

git checkout .\n<\/code><\/pre>\n

Undo git add for at single file:<\/p>\n

git reset folder\/file.cs\n<\/code><\/pre>\n

Undo git add .<\/code> :<\/p>\n

git reset .\n<\/code><\/pre>\n

Fix untracked files<\/h2>\n
git rm . -r --cached\ngit add .\ngit commit -m \"Fixed untracked files\"\n<\/code><\/pre>\n

Create 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>\n

You can now just type git a<\/code> to add unstaged files.<\/p>\n

Aliases can also be added directly to the config file.<\/p>\n

[alias]\n    a = add .\n    c = commit\n<\/code><\/pre>\n

Share this:<\/h3>