DEV Community

loading...
Cover image for Next step Git for those that already know add and commit

Next step Git for those that already know add and commit

t4rzsan profile image Jakob Christensen Originally published at leruplund.dk on ・3 min read

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.

The list

In no specific order whatsoever.

Edit configuration

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

Set editor for commit messages

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.

Set merge tool to Visual Studio

[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

Submodules

Cloning submodules

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

Change submodule to own fork

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).

git checkout

If you have trouble downloading the code for the submodule, try running the command:

git submodule update --remote

Start merge tool

If there is a merge tool, you can start your merge tool (set in the config file).

git mergetool

Compare to remote

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:

git merge

Show remote URL

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://hocuspocus@bitbucket.org/myteam/myproject.git

Delete branch

Delete the remote branch:

git push -d <remote_name> <branch_name>

For example:

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>

Delete local changes

Undo all unstaged local changes:

git checkout .

Undo git add for at single file:

git reset folder/file.cs

Undo git add . :

git reset .

Fix untracked files

git rm . -r --cached
git add .
git commit -m "Fixed untracked files"

Create an alias for a command

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

Discussion

pic
Editor guide
Collapse
ghost profile image
Ghost

This is actually the best! Thank you so much!

One question, though: with the aliases, is their anyway to make it so that it accepts terms? i.e. I want to make it so I can do $ git c "commit message", and it'll action that as $ git commit -m "commit message". Is that possible?

Collapse
t4rzsan profile image
Jakob Christensen Author

You are welcome :)

Aliases are really just a search and replace. So you can do

git config --global alias.cm "commit -m" 

and then


git cm "My commit message"

will be replaced with

git commit -m "My commit message"
Collapse
ghost profile image
Ghost

Ah, gotcha! Thank you so much again!

Collapse
maxdevjs profile image
maxdevjs

Nice list, thank you. I found some new things to try out :)

At some point, this became one of my favorite configurations:

[credential]
  helper = cache --timeout=...
Collapse
polentino911 profile image
Diego Casella

For those who use a lot the CLI, I would highly recommend also to install tig - a text mode interface for GIT.
There's also a good introductory blog post by the guys at Atlassian, which is also how I stumbled upon this handy project.

Collapse
t4rzsan profile image
Jakob Christensen Author

Thanks for sharing, Diego. I did not know about Tig.

Collapse
eljayadobe profile image
Eljay-Adobe

TFS originally had only TFVC. Now it has TFVC or Git for version control.

TFS is a big umbrella -- a suite of tools for ALM, including version control.

TFS/TFVC is just the original version control part. (1)

TFS/Git is just the recently added version control part.

The one thing that, alas, TFS does not do is bridge TFS/TFVC and TFS/Git. They are separate version control subsystems, and do not interoperate.

Some people have created various TFS and Git bridging utilities, but they have their own quirks and problems. From my coworkers who used them, the pain was not worth the gain. (2)

(1) TFVC was created by Brian Harry, the same person who created SourceSafe, and who inadvertently created what eventually became .NET because he hated COM. The origin tales of .NET are crazy wonderful, and show how innovation can come from anywhere.

(2) My coworkers were no slouches at TFS (the ALM), or TFVC, or at Git, or at another version control system called Source Depot only used internally. Because at that time, my coworkers were Microsoft coworkers, and we worked on a product called Visual Studio.

Collapse
rupeshiya profile image
Rupesh Krishna Jha

git rebase -i HEAD~n can also be added . But it was really good.

Collapse
tmcsquared profile image
TMcSquared

This is an awesome compilation Jakob. Thanks for taking the time to write this stuff down.

I could use those aliases pretty well, :) I didn't even know it was possible!

Collapse
t4rzsan profile image
Jakob Christensen Author

I'm glad it was of use to you. Thanks :)

Collapse
letsbsocial1 profile image
Maria Campbell

Fantastic post Jakob. Thanks for sharing!

Collapse
t4rzsan profile image
Jakob Christensen Author

Thanks Maria. I appreciate it.

Collapse
bugsenpai profile image
bugsenpai

thank you it work as well