Meet HubJur

How to use Git and GitHub: a step-by-step dummies' guide

Have you ever heard about Git? And GitHub?

Today we will present you a simplified step-by-step guide of this two amazing tools, my dear tech-enthusiast friend. We will go through the main concepts, how to use Git to manage your source code and how to create a GitHub profile so that you can publish your online coding portfolio.

Even if you are not a programmer, this is a great opportunity to learn how does Git and GitHub work, internalize some key concepts and become familiarized with this incredible tool which will contain all our later codes and snippets here, so that you can keep following our content in the future.

I also present to you guys and girls the video I made on this topic from my YouTube channel, which is where I share some thoughts, tips and projects I am currently working on with other tech-fanatics, whether you are a developer, lawyer or enthusiast. Please also consider subscribing to my channel to not miss future videos and to join our live "coding sessions" and a nice, relaxing tech-related chat -- I promise to keep things interesting. The content is in Portuguese, but you can watch my videos with subtitles (maybe I will switch to English in the future, if this makes sense).



All set? Follow me then.


Git: a code version-control software¶

In the development world it is good practice your project to be version-controlled, that is, that you can "archive" all changes made to your code throughout its life in separate versions.

That way, any developer can quickly spot any changes made, receive contributions in other parts of the code from third parties and have a backup of the code in a convenient and detailed manner without the need to locally save a number of folders and files with names such as "v1.0.0.1", "Jul272921.zip" or "initial version", as I used to do back in the day. Before meeting Git, it was common place to search in external HDs the version I was using before and sending some backups to my e-mail from time to time. A real mess.

Habemus versions?



As such, meet Git, a computer software designed for version-control and used to manage code all around the world.

Git was born in 2005 by the hands of Linus Torvalds ("father" of Linux) to be used in the development of Kernel Linux (Linux "core"), being later adopted by other projects. [Veja aqui mais informações sobre o Git]

At my first job at in the tech field (around 2003), confusion was very common when more than one co-worker tried to edit the same file, sometimes compromising other parts of the system in the process. Using Git, this problem is no more.

Git makes possible to centralize all of the project's code and track changes made by one or several people. Thus, whole teams could work at the same time in the code without bumping heads with one another, also enabling huge projects to evolve at an amazing pace. Even you can contribute today with any open source project creating a functionality not yet made and suggesting ("pull requesting") your changes to the project owner through Git. We will see how to do this in details.


What about GitHub?¶

GitHub is a platform which enables you to use Git in the "cloud". In other words, it is a website that grants you the power to create and store code, which will be available and that can be updated online. There are alternatives to GitHub such as BitBucket (which was always my choice for private projects) and GitLab, both excellent, but we will talk about GitHub here, since it is the most used one.

You may (and should) create your GitHub profile for free, accessing github.com and clicking in the "SignUp" button. 

Tela inicial do GitHub
GitHub


Once registered, you can create your first repository clicking in your profile photo and in the "Your Repositories" link. Finally, click on "New" and choose you repo's name, set if it will have public or private access and that should do it!

A repo works like a "folder" for your files. Once you create ir, you can send files and updates through Git and it will track your changes.

You will find a huge number of public repositories in GitHub (such as dev.lawyer's ones) which you can access, copy, use and contribute code whenever you deem convenient.

Apart from a backup and versioning platform, GitHub allows you to find collaborators for your project, help others in projects that interest you and use GitHub as your development "portfolio", since anyone can check your public profile to know more about you as a programmer. I dare to say that for programmers, a solid GitHub profile might be more important than a good LinkedIn one. Many recruiters out there ask for your GitHub public profile link as a mean of knowing you better code-wise.



Have you created your public profile? Follow me on GitHub then so that we can share some pointers.

My GitHub profile: https://github.com/OctavioSI

Dev.Lawyer's in GitHub: https://github.com/Dev-Lawyer

Link for this content's repo: https://github.com/Dev-Lawyer/GitEGitHub



How does Git and GitHub work in fact and how to use them?¶

The best way to learn is doing it. First, download and install the latest Git version in your machine, which can be found here: https://git-scm.com/downloads

After installation, you will have access to a Git terminal (usually it is pre-installed in most Linux distros and, if you are a Windows user, it will be found under the "Git Bash" program). So, we will use the terminal for commands -- don't be intimidated by it though, since once you are used to the basic commands, everything will be clearer.

Now we will create a new GitHub repo. I will create my repo in Dev.Lawyer's profile, and you should make the necessary adjustments here. Our first repo will be called "GitEGitHub", and we will only have a README.md file (file that contains our repo's info).

Creating a new repo



Once you repo is created, you can then locally "clone" it in your machine. Keep reading, since we will better explain its mechanics and repos' flow but, in sum, this means that we will make a local copy of our remote repo that is hosted by GitHub, in the cloud. You will find in the repo main page a "Code" green button, which will present the "clone" option.

Cloning a repo



You can directly clone a repo using Git, using the following command (replace this address with the address of the repo you created, which is in the "clone" HTTPS bar in the picture above):

git clone https://github.com/Dev-Lawyer/GitEGitHub.git

Since our repo is public accessible, we can clone it without any authentication issues. Should your repo be set to private, you will need to enter your credentials when asked.

Cloning a repo locally

After cloning our repo, we can now access our local files and modify their contents. You will realize that there is ".git" folder which contains some info on our repo (this indicates that we are accessing a "Git managed" folder), a "LICENSE" file (which contains the license we set for our repo) and finally a README.md, all created by GitHub.

Our repo locally cloned



Let's change the content of the README.md file so that we will include the permalink for this content. We can do that in any text editor we want.

After changing and saving the file, we will follow this steps:

  • Using the terminal (or Git Bash) we will access our local repo's folder (you can navigate to our folder using the "cd" command);
  • Once at the appropriate folder, let's add all modifications to our track changes using the command "git add -A"
  • Now we will "commit" this changes which actually means that we will inform Git that we are creating a sort of "checkpoint" in our code, a new version of it. We also include a -m '<message>'  command, so that we can set the message our commit will present.
  • We will not need it in our example, but should we need to download any changes made by others in the existing code hosted at GitHub before submitting our changes, we would need to use "git pull";
  • If there are any conflicts between our changes and the remote version (that is, changes in files that overlap and are incompatible), we would then solve such conflicts indicated by Git and then commiting a new merged version, in which the remote code would be then "fused" into our local changed copy. After everything is set, we would then push our code to the remote GitHub repo, using "git push origin" (we will push to "origin" which is the remote repo set, which will then have the same commits we locally have).

Commiting our local changes to GitHub's remote repo


We used the following commands in our code above:

cd ~/Projects/devlawyer/GitEGitHub/
git add -A
git commit -m 'Atualizando o link do artigo'
git pull
git push origin

If we access our GitHub's repo now, we will see that the README.md file was indeed changed and now reflects the updated link we have provided.

It worked :)

Clicking in our past commits, we can also see the content of each file at that moment. 

This is Git's basics, but we can surely make a lot more than that (and I can make more content on this in the future, if there is enough interested readers), such as:
  • creating sub-divisions of our repo to deal with a funcionality, bug or isolate feature (creating a "branch" of it);
  • creating separate test and productions environments, so that the code can be tested before reaching the final user;
  • defining workflows for teams to simultaneous work together, using approvals and revisions pipelines;
  • structuring automated routines to validate the code, avoiding errors before the code reaches the production environment; e
  • "pushing" code to other repos, collaborating in third parties' code.


Does it sound too complicated?

Muito complicado?


Does Git only work for code, or can I version-control any document?¶

Yes, Git is not limited to code! In fact,Git can be used to track the whole history of changes in any kind of document.

Note that there are a number of tools that use the version-control concept for files such as excel and word documents, such as GitPitch and Simul, even though there are limitations to work with "closed source" files such as .xlsx ou .docx, which make this solutions less functional than the version-controle we use with bare code.

Even so, it is possible to list a number of advantages when using Git to version-control documents: you can use it to have a more reliable backup of your documents when comparing to storing them in your hard-drive, and you can also track who changed the file at some point in time. This is debatable since Dropbox and Google Docs, for example, can track versions of your documents today, but we still second that Git could be used for something more as regards documentation.

We will certainly explore a lot more on this topic, since our "legal" audience.


This seems a lot for version-control alone. Is it worth it?¶

When we look at GitHub it is clear that its value is far above code storage and version control. The platform has become, in fact, a huge collaborators community. When making available code so that one or more individuals can learn, use and share/improve/amend/complement it, GitHub has become a great space for shared economy.

One way or another, reading about Git will be great for you -- I am constantly thinking on how to integrate technology and Law, and this is the first step if want to follow our content here (since I will make code available in GitHub).


/* End of this post, see you next time */
How to use Git and GitHub: a step-by-step dummies' guide How to use Git and GitHub: a step-by-step dummies' guide Reviewed by Octavio Ietsugu on July 27, 2021 Rating: 5

No comments:

Powered by Blogger.