Git ready in 20 minutes!

Git ready in 20 minutes!Shivangi PatelBlockedUnblockFollowFollowingJan 23When I began my journey towards Data Science, a close friend and mentor advised me to get familiarized with git.

It seemed like a distraction, but I thought, “It’s a version control system.

Surely I can learn in 2–3 days and return to the beautiful world of Python.

” I couldn’t have been more wrong.

My 2 days short trip away from Python turned into a 3-month vacation.

I certainly do not regret the time spent, as there is no denying the importance of learning git.

The significance of git for every programmer has been well described in several excellent blogs and I am not going to reiterate the same here.

For a beginner like me with no programming background, it may look intimidating at first.

After spending a lot of time behind trying to understand the underlying concepts and internals of git, I have realised that there is really no end to it.

However, it can be learnt in stages.

If you have never used git and have been putting it for later, I strongly recommend you to at least get started.

It isn’t as complex as it seems and you can always keep learning more features on the go.

By the end of this article, you will have a decent understanding of what git can accomplish, created an account with GitHub, taken security measures, created a repository and checked in some code — all hopefully in 20 minutes!Let us begin with a few key concepts.

GitGit is a version control system; a distributed one.

A version control system (VCS) records information on — what, when, where, and by whom the changes were made to a code file.

Unlike other VCSs, git is ‘distributed’ i.

e.

, it is not administered by a centralized server.

Git stores data in the form of snapshots.

When you save the state of a file (known as commit in git terms), git takes a series of snapshots of the file and assigns it a unique 40-character key.

During the next commit, it again takes a series of snapshots.

If any changes were made, a new key is generated.

If not, the file is referenced using the same key.

Thus, at its core git is nothing but a key-value data store.

At its core git is a key-value data store.

A persisted map.

GitHubGit protocol is hosted by several service providers like GitHub, Bitbucket and Codebase to name a few.

GitHub — recently acquired by Microsoft, is one of the most popular git hosting platforms.

Now you know why you can’t use the terms git and GitHub interchangeably.

RepositoryA git repository is a collection of all files and folders associated with a project.

Simply put, repository is like a directory containing folders, sub-folders and files.

GitHub allows you to create unlimited numbers of public and private repositories.

As the names suggest, public repositories are publicly accessible to view and copy; while users can exhibit access control for their private repositories.

SSH keysSSH or Secure Shell allows establishment of a secure connection between a host and client using cryptographic techniques.

Generation of SSH key is essentially generation of a public-private key pair.

The motivation to use Secure Shell authentication is to considerably enhance the security of information transfer between two parties (your computer and GitHub in this case).

For a better understanding of SSH protocol, refer to ssh.

comCommand Line Interface (CLI)CLI allows you to interact with your computer through commands.

Unlike Graphical User Interface (GUI) where you can click on graphical icons using a mouse, interaction using CLI is faster, reliable and allows advanced operations.

If you have never used Terminal before, now is the time to start.

There are just a few commands required at this stage.

Please feel free to skip this section if you are familiar with using a CLI.

Open a Terminal window and type the following commands to understand how to navigate within folders and files.

You type a command at the cursor and hit Return to execute the command.

lsList contents of a directory.

Your directory may contain files and folders.

Once you type ‘ls’ and hit return, all the contents are listed.

ls –aList contents including hidden files and folders.

cd <folder_name>cd stands for ‘change directory’.

Type the name of a folder/directory you wish to navigate into ( do not type <>).

If the destination folder is not located in the current directory, you need to type the entire path to the destination folder; or use the command cd .

cd .

Navigates one step up in the directory.

pwdprints path to the current directoryclearclears screen of all previous commandscp <source/filename> <destination>cp stands for copy.

To copy a file from one location to another, use the above format.

That is enough to get started.

I am assuming here that you are using a MacOS or Unix based system.

You are familiar to basic Terminal commands and are able to navigate to desired directories.

Create GitHub accountCreate an account with GitHub by signing up.

You can choose the Free plan at this stage.

Verification of email address will navigate you to the home page.

Generate SSH keyYou can use an existing SSH key with GitHub if you want, in which case you can go straight to the next step — Add SSH key.

To generate new SSH key, go to Terminal, execute the command ssh-keygen.

A message of ‘Generating public/private rsa key pair’ will be prompted.

Just hit return to accept the default location and file name.

If the .

ssh directory doesn’t exist, the system creates one for you.

Enter, and re-enter, a passphrase when prompted.

Execute the command ls –a to list the hidden folder called .

ssh.

List contents of .

ssh by ls command.

Three files will be listed, of which id_rsa contains the private key and id_rsa.

pub contains public key.

Display the public key on-screen by executing command cat id_rsa.

pub, and copy the key from the Terminal window.

Add SSH keyGo to your GitHub account online.

Click on the dropdown menu next to profile icon.

Click on Settings and navigate to SSH and GPG keys.

Click New SSH key button, add a title (ex: id_rsa.

pub) and paste the public key copied from Terminal window.

Note, you should NEVER share your private key.

You have now enabled SSH access to securely manage git operations with GitHub.

Add new SSH key on GitHubCreate a repositoryNavigate to GitHub homepage.

On the left panel, click on the link to create new repository.

Assign a name , ex: ‘my-python-notebooks’ to your repository and provide a one-liner description.

Select Public, Initialize repository with a ReadMe and click on the Create Repository button.

Create a repository on GitHubClone your repositoryThe repository that you just created on GitHub exists at a remote location.

To be able to work on it in an offline mode, you need to clone the repository.

This enables you to create a copy of the repository on your local computer and sync between the two locations.

On GitHub, under the repository name, click on Clone or download.

In Clone with SSH section, copy clone URL for the repository.

Clone your repositoryGo to Terminal, change the working directory to the directory where you want the cloned repository to be saved.

Type git clone, and then paste the url you copied earlier and hit return.

The repository on GitHub has now been cloned to your computer.

git addNow that you have your repository in your local computer, you can copy any number of folders and/or files into it.

For ex: copy an existing Jupyter notebook filename.

ipynb using the cp command.

Once you have the filename.

ipynb in your local repository, execute git add <filename.

ipynb>.

The git add command snapshots the file in preparation for versioning.

Note that this file is not yet ‘versioned’.

This step ‘stages’ a file for the commit.

git commitTo commit the staged file, execute the command as follows:git commit –m“The file contains basic Python datatypes.

”The comment within quotes is a descriptive message about file contents or modifications made by the user.

This step records file snapshots permanently in version history.

git pushExecuting git push command uploads the commits to GitHub.

You can now refresh your browser and confirm that the file is versioned in your repository on GitHub.

Congratulations!!!You have successfully created your first repository and checked in your file to be tracked by git.

What’s next?I would recommend you get a solid hands on with creating repositories, adding and/or editing more files and commit the modifications.

Next, you should be able to initiate git tracking on an existing directory on your local computer.

Further, understand the concepts of branching, merge, pull requests and deployments.

These processes are implemented while collaborating with a development team.

.. More details

Leave a Reply