Fundamentals of Git and GitHub

Get started with the fundamentals of how to use Git in the Command Line Interface and how to get your code into GitHub repositories.

Diego Monroy
5 min readMay 19, 2021
Photo by Roman Synkevych on Unsplash

Git is a source control system very popular and adopted by many in the industry, is a distributed system and is open source and free to use, you can work disconnected, it is powerful and easy to use, here we are are going to see an overview of how to start using Git and GitHub.

The most powerful way to use Git is using the Command Line, although there are GUI interfaces like Git Gui, GitHub Desktop, Source Tree, Git Kraken, and many others, git shines using the command line, so it is important to get familiar with the CLI (Command Line Interface).

Installation

Git works on Linux, Mac, and Windows.

Check if you have git installed in your machine running this command.

foo@pop-os:~$ git — version

If you don’t have it, download Git, also use and download an editor of your choice it can be Visual Studio Code, Sublime Text, Atom, or any other of your preference.

I will be using a Linux OS distribution called Pop!_OS that is build on Ubuntu, so let’s dive in.

Configuration

The first thing to do after having installed Git, is to configure it, use your name, email and check that the changes were made.

foo@pop-os:~$ git config --global user.name “Your name”foo@pop-os:~$ git config --global user.email “youremail@host.com”foo@pop-os:~$ git config --list

Fundamentals

In a distributed source control system, everyone would have the entire project and full history in their machine, this makes possible to work offline, and no dependence on a central repository, is normal to have GitHub as a central repository for our projects, so everyone can push and pull from there.

The 3 states of Git

  1. Committed: The data is store locally.
  2. Modified: The file has a change.
  3. Staged: The modified file is part of the next commit snapshot.

The 3 areas of Git

  1. Working directory: Contend is created, modified, and deleted.
  2. Staging area: Where changes from the local directory will be staged before committed.
  3. git repository: Local git directory.

There is a 4th area to have in a central place that we can call the Remote repository, that could be GitHub.

Git flow

All interactions with git from the command line start with git.

For configuration as we did before.

foo@pop-os:~$ git config

Initialize a git local repository.

foo@pop-os:~$ git init

or

foo@pop-os:~$ git init [ project name ]

Download a repository from remote.

foo@pop-os:~$ git clone [ repository ]

Prepare a file or files into the staging area.

foo@pop-os:~$ git add .

or

foo@pop-os:~$ git add [ file or files ]

Commit a file to the local repository.

foo@pop-os:~$ git commit -m “Message to go with the commit here”

Example

  • Create new folder, use mkdir for creating a new folder where you want to create your repository.
  • Move to the directory, use cd to move to the newly created folder.
  • Use git init o initialize an empty git repository, by default, you are going to be in the master branch or the newly called main branch.
  • Check files, use ls -la to see the hidden files and you will see a .git directory created by the init command, which is the directory where git keeps track of all files in the directory.
  • Use git status to check the status of the current repository.
  • Open your editor in this location and create a new file, you can name it readme.md that is used to describe your repository.
  • Save your changes.
  • Use git status, now it shows an untracked file, the readme file is in color red.
  • Use git add readme.md, this will add the file, git add . is in case you have many different files changed, but be careful, is used only if you are sure what files have been changed and you want to commit.
  • Use git status gain, now the file is tracked, and the readme file is in green color.
  • Use git commit -m "[DOCS] initial commit" to commit the changes.
  • Use git status and now there is nothing new to commit.
  • Use git log to show your commits.

Fundamentals of GitHub

GitHub is a hosting site service based on Git and is widely used by many developers for small and large projects, there some free and paid versions. It is built for collaborative projects as a social aspect of coding and it’s own by Microsoft.

Pull requests is one of the features that makes GitHub shines, that combine a code review, where different developers propose changes to existing projects, and make the community to engage.

First Steps

  1. Create a GitHub account.
  2. Configure your profile.
  3. Use ssh. Connections options are HTTPS that requires username and password and SSH that is easier to work to communicate in a secure way.
  4. Create a SSH key.
  • Check keys in the root directory.
foo@pop-os:~$ ls -al ~/.ssh
  • Generate key.
foo@pop-os:~$ ssh-keygen -t rsa -b 4096 .C “youremail@host.com”
  • Accept the default location.
  • Add a passphrase if you want extra security.

5. Add ssh to ssh-agent.

foo@pop-os:~$ eval $(ssh-agent -s)foo@pop-os:~$ ssh-add ~/.ssh/id_rsa

6. Copy the public key that you can find in ~/.ssh/id_rsa.pub. You can open that file with an editor and copy the contents of the file.

7. Go to GitHub and open settings, next click on SSH and GPG keys, and add a new SSH key, give a title to identify if it is your computer, or work computer, and paste it into the key box. Click on add SSH key and enter your GitHub password.

8. In your terminal type the following code to authenticate, and type yes.

foo@pop-os:~$ ssh -T git@github.com

That’s it, we are now authenticated to connect by ssh with our GitHub, congratulations!

Repos

Repositories are the building blocks of GitHub, first create a repository and clone it into your local, it can be public or private, only collaborators can change the code

Overview to work with GitHub and Git

  • git clone, a full copy of the remote repository in the local directory.
  • git add, track the files.
  • git commit, store changes in the local directory.
  • git push origin (master/main), push to GitHub. Here it could conflict if there are new changes in the remote repository (GitHub repo).
  • git fetch and git merge to pull the most recent changes from remote repository or git pull to do both command ( git fetch and git merge).

And that’s it, now you know the basics of working with Git and GitHub.

Additional Content

--

--

Diego Monroy

Frontend Developer💻, Holberton School 🎓, Zen ➡️ Code ➡️ Read ➡️ Debug ➡️ Repeat, Peer pressure can change opinions and even perceptions💡