Day Two - Let's move our first steps with Git

“Bite my lip and close my eyes…take me away to paradise.”

Heyyyyyy,

Let’s move on to another session of our Git Journey! In the previous blog post we made a quick introduction on Git and Github, now we will move our first steps with Git. We are going to learn the commands you will use all the time. We will learn how to add code to our repositories, push or pull changes from/to our repos and from/to our machine.

Agenda


Git Workflow

Before moving away, let’s see other few key concepts:

Working Directory:

The working directory is essentially your project folder

Staging Area:

Is a particular area where the files are copied and are ready to be committed to our repo. When you put a file in the staging area you are telling git to include it in our repo in the next commit. Files that are in staging area are not directly moved to the repository but you have to push them on it.

Repository:

Is our big box where you commit your final files. This repository then can be pushed to Github. Is essentially the hidden .git folder in our working directory.

Git Add

git add "file"

With this command you will add you untracked files from your working directory to the staging area. In this way they will be ready to be commmitted. You can also specify multiple files that we need to add to our staging area by add the filename separated by space.

If you want to move all the files from the working directory to the staging area, you only need to launch this command:

git add .

Git Commit

git commit -m "commit message"

With this command we inform git that we would like to move our files from the staging area to our repository in order to create a checkpoint for future uses. I said that because if we need move back to a specific version, we can restore a previous commit.
A git commit is not intended as a saving changes in a single file but can constitute a set of changes across an entire working directory.

Git Log

git log

This command will show a list of all the commits we made to a specific repo. It will display you a set of information like a commit identifier, the author of a certain commit, the date in which the commit was executed, on which repo the commit has been done, the message we used when we performed the commit

Push code to a remote branch

When you create a repository on Github you can see the command you can use to add the origin to your local environemnt like:

git remote add origin https://yourgithubrepo.git

You can alse remove the connection from your local environment to the remote environemnt using this command:

git remote remove reponame

Whith the command below, you will check the connection between your local environment and your remote branch. If you didn’t add the remote connection with the command above, you won’t receive any result.

git remote -v

The remote connection will allow us to operate on our remote repository and push there the code from our local environment.

When you create a remote repository on Github you will receive a bunch of commands you can use to create a local repo from command line:

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://yourgithubrepo.git
git push -u origin main

more or less we explored quite all the commands above…but why do we need to run the following one?

git branch -M main

If you search for this command on Google you can see the reason explained directly by Github:

“Many communities, both on GitHub and in the wider Git community, are considering renaming the default branch name of their repository from master. GitHub is gradually renaming the default branch of our own repositories from master to main. We’re committed to making the renaming process as seamless as possible for project maintainers and all of their contributors”

When you try to push the code from your repository to your remote Github account you have to launch the following command:

git push -u origin main

The command above means “hey man, let’s push what you have locally to the origin branch that is the remote name of the branch and do it the main branch”.

Pleae note that you can encounter some difficulties authenticating yourself from the command line. After few tentatives for which i received the following error:

fatal: Authentication failed for 'https://github.com/my-repository’

I’ve understood that the password for which I’ve been asked was my PAT!!! (If you do not know what is a PAT you missed my previous blog post! damn yourself!!)

Retrieve code from a remote repository

Now we’ve seen how to push code on a remote repository, it’s the turn of retrieving it back.
You have two options to retrieve changes from a remote branch to our local machine:

git fetch origin <branch>
git pull

with git fetch command you grab the remote commits you pushed on your remote branch and retrieve them on your local repository .git file (remember that is different from your working directory). This command wont update the files in the working directory. You can check/read changes using the “git log” command.
This is mainly done to avoid merge conflict between your local repository and your remote repository. When you work with other people, maybe you only would like to check what have been made without merging or overwriting your latest work.

After you performed a git fetch request, you can also run

git log
git checkout origin/main

To see which are the updates made but always in “detached HEAD” state (no overwrite of files). To get back to the previous situation, you can run:

git switch main

git pull will grab the commits from your remote repository and push them on your working directory. With this command you’ll update the files to match up the remote branch.

In general, before pushing to a remote repository, it’s better to be in sync and perform a pull request.

Let’s put them together! Hands on!

Ok, guys now that we made a little bit of theory let’s practice.
Let’s say we would like to do the following:

  1. Create a new local repository and create a new local file
  2. Create a remote repo on Github and push local changes to the new repo
  3. Create a remote file on Github and Fetch/Pull changes from remote to local machine

CREATE A NEW LOCAL REPOSITORY AND CREATE A NEW LOCAL FILE:

Open up the terminal on your machine and do the following:

cd Desktop
mkdir TestGithubOne
cd TestGithubOne
git status
git init
touch testfile.txt

CREATE A REPO ON GITHUB AND PUSH LOCAL CHANGES TO THE NEW REPO:

Open up your browser, let’s login to Github and create a new repository with the same name as the one with created above (TestGithubOne). Let’s make it Public, no needs for license, readme files and other stuff. Let’s simply create it.

Now, Link the remote repo with your local repo. From the terminal just launch:

git remote add origin https://YOURPAT@github.com/YOURUSERNAME/TestGithubOne.git
git add testfile.txt --> to add this file to the staging area
git commit -m "this will be my commit for this hands on"
git push -u orgin main

In the previous steps, if you forgot to add the PAT to the “git remote add origin” command, you will receive an unauthorized error when trying to push the files on Github. If you try again to add the remote connection to the local machine, you will receive an error message that tells you that a remote origin already exists.
In this case you need only to remove the origin and try again all the commands listed above. To remove the origin you need to launch the following:

git remote remove origin

CREATE A REMOTE FILE ON GITHUB AND FETCH/PULL CHANGES FROM REMOTE TO LOCAL MACHINE

Now we are quite done!
Create a new txt file named “mygithubfile.txt” on your Github repository and commit it.

If you only wants to check files that are on your remote Repo without adding it to your working directory you can do this:

git fetch origin main
git log
git checkout origin/main
git switch main

Then you can launch the following command to finally retrieve the files from Github and have those ones in your working directory:

git pull