Let's GIT started

Thursday, September 26, 2019

There is a lot to discover about Git. Every programmer utilizes it, and it should come as no surprise that it is VERY useful. Most people use it through their IDE, making it child's play. But if you really want to unravel all the possibilities, you need to dive deeper. I'll be going over some of the basic commands, using Git Bash. Which stands for Born Again SHell, if you did not know!

Please note that this won't be a complete how-to-guide, but rather a quick glossary, leading to the more interesting examples in which we'll be using shell commands.

On the other side, if you aren't up to speed, download Git from https://git-scm.com/downloads and look up how to connect your local and remote repository with SSH-keys here: https://kernelmastery.com/ssh-key-authentication-disable-root-password-l...

Great. Now, let's go over the basics real quick, yes? Good! If you'll pardon the pun; Git going then:

Navigating towards the folder you need isn't that hard. The command cd followed by a space allows you to type the path. Or just type the first letters and hit tab. Git will auto complete it for you, folder by folder. Once you're in the directory where your project's located enter git init to create a local repository. You'll notice that you're now on the master branch.

Of course, there must be a remote repository as well. GitHub is one site that serves as such. If you don't have an account there, create one now and follow the steps in order to initiate a new remote repository.

Moving on. Don't forget your .gitignore file! Being a console-command fan, here's a little extra help for that: https://help.github.com/en/articles/ignoring-files

If you want to add files that you'll want to save to your repository, move down to the folder where they're located and type git add file.fileExtension.

Else, if you don't need to specify a file, simply type git . The . will add anything below your current folder. Use the shorthand cd .. to move up one folder at a time, instead of typing cd path again.

By committing, you're creating a record of what files have changed in your repository. In case you mess up and need a rollback, you can always reset. Don't forget to use the flag -m, so that you can add a message to your commit. Make it short, but descriptive. Your fellow team members will thank you for it. There are several ways to link your remote and local repository. I choose the one below.


All that's left, is to push your local commits towards your remote repository. Don't forget to set your remote origin as master branch. Note that origin is a git alias for your remote repository. In my case it references henceforth to git@github.com:Paul-Gerarts/gitBasics.git.  

There are several commands that help you even further. As you can see, they're quite self-explanatory. Adding flags to them, allows for more flexibility. Take git log for example and let us add a flag: git log --graph. It will show you your branches and where they merged. I encourage you to try that one out on your own project.

I won't take up any more time refreshing the basics, and I won't delve deeper into resolving merge conflicts through Git (yes, you can). Instead I'd like to introduce you to Git aliases. Some are easy to create:

Simply call upon your git config --global and announce that you're going to create an alias. after which you can type your own shorthand. Leaving a space in between, you then state the command you want to create an alias for. Easy? Very much so. Useful? Not in this case. Abbreviating a six-letter word to a two-letter one isn't worth the time to configure.

Hello world, and hello shell functions! Putting an exclamation mark in front of your alias-to-create, you expand towards your shell and will be able to use standard unix tools for some crafty coding.

Basically, you'll need to know:

function: 'f( ) { } ;  f'

parameters: $1, $2 and so on...

enclosed shell commands: $(       )

Remember those and we'll be working magic in no time. Behold:

This is a step up already. One simple function now allows us to use a shorter command to which we're able to pass arguments into. Might not be as clean as a whistle, but it works. We could refactor of course. Using the same alias.shorthand overrides our previous abbreviation and we'll be able to enter a new function. Try it yourself or move along onto the next. Combining commands with logical operators:

You can read what went wrong. In a feeble attempt to make my life easier, I forgot to account for possible errors. Nothing to find in the remote repository, so there's nothing to delete. Well, time to put on your blue hat, because we're about to refactor this one.

There we are. Let's break it down from left to right, shall we? What were the ingredients?

> git config --global alias.purge we know by now. It's the code we need in order to create an alias.

> 'f() { we start to write our function.

> if [ the start of our if-statement, closing it later on with ].

> git ls-remote --heads origin $1 checks if there is a remote branch with the name we supplied.

            Return value would be nothing if there's no branch, otherwise, it'll return the reference tag.

            By enclosing it in $(   ) we make sure it's known to Bash as a command.

> | wc -1 added as a suffix, converts the return value to 0 if there's no remote branch, 1 if there is.

> -eq 1 turns the whole enclosed command into a boolean, read as (returnValue == 1)

            // NOTE: because the commands are written on one line, we need to type ; after each one.

> then is used to announce what our function should do, if the boolean returns true.

> git branch -d $1 will delete the local branch of the parameter we entered.

> && this logical operator will ensure both commands are carried out.

> git push origin –delete $1 will delete the remote branch of the parameter we entered.

> else does what the second half of an if-else should do:

> echo "remote branch not found" is a String I want to return in case there's no remote branch.

> fi announces the end of our if-else statement.

> } ; f' closes our function

There are many command line shells out there, but this one remains my favourite. I hope you were able to learn something from this short read, even if it was only one miniscule thing. Don't hesitate to create as much aliases as you want, in order to work faster and save you a bit of time.

And if you ever forget your shorthand, just type git help --all to find your command aliases. Just keep scrolling down. 

 

--commit early, commit often-- a tip for version controlling – not for relationships