Introduction to Git

Simply put, Git is the tool that tracks the changes in your code over time while GitHub is a website where you host all your repositories.

Git is a free and open-source Version Control System.

GitHub is a git repository (server) hosting service and it's a company that allows you to host a cross-platform repository in a remote serve.

A repository is a project, or the folder used to organize one single project. It can contain folders, files, images, videos and spreadsheets.

Version Control is the management of changes to documents, computer programs, large web sites and other collections of information.

Version Control System is a software tool that helps record the changes made to the files by keeping track of changes made to the code so a specific version may be called later if needed. This ensures that all team members are working on the latest version of the file.

Git Commands

Creating a repository

--creating an account on GitHub

--creating a Personal Access Token on GitHub

--create a repository

Clone a repository

We clone a repository in order to copy the repository on GitHub to our local machine.

git clone project_ssh_url

--create a file

--push the file to GitHub

 

Objectives

What is source code management

What is Git

What is GitHub

What is the difference between Git and GitHub

How to create a repository

What is a README

How to write good READMEs

How to commit

How to write helpful commit messages

How to push code

How to pull updates

How to create a branch

How to merge branches

How to work as collaborators on a project

Which files should and which files should not appear in your repo

.gitignore file

Creating a New Repository

  • Initialize a local directory so that it becomes a git repo
git init
  • Add files in your local repo to stage them for commit.

To add a single file:

git add <file_name>

To add all the files:

git add .
  • Commit the file to be tracked for changes and prepare it to be pushed to a remote repo
git commit -m “<meaningful_commit_message.>”
  • Rename your branch from master to main
git branch -M main
  • Add the URL for the remote repo where the local repo should be pushed
git remote add origin <remote_repo_url>
  • Push changes in your local repo to the remote repo
git push -u origin main

Git Commands

Cloning a repo

We use this command to download a repository hosted on GitHub onto our local machine

git clone <remote_repository_url>

Tracking files

We use git add to track files and changes using git.

Track a single file:

git add <filename>

Track all the files, we use a period

git add .

Commit

saves files in git.

git commit -m “<meaningful_commit_message>”

-m represents the message that should have something to do with what and why you're making that commit.

You can use more than one -m where the first will represent the title of the commit and the latter, the description of that commit.

git commit -m “<title_of_the_commit>” -m “<description_of_the_commit>”

Push

Uploads git commits to a remote repo like GitHub

git push origin main

To avoid having to use origin main each time we push, we can set where we want to push by default and this is called an upstream

git push -u origin main

-u stands for upstream and is the same as using --set-upstream

Pull

Downloads changes from a remote repo to your local machine.

git pull

Status

Shows all the files that have been added, modified or deleted but haven't been saved in a commit yet.

git status

Remote

Shows which remote repository is connected to our local repo

git remote -v

Origin is the short name for the URL. When you use the name origin, you're referring to the GitHub URL like an alias name.

The name of the remote URL can also be changed from the origin to any other name. 

To add a remote origin:

git remote add origin <remote_repo_url>

To rename the remote URL:

git remote rename <old_name> <new_name>

To remove the remote URL:

git remote remove <name>

Diff

Shows the changes made after comparing two versions of code.

git diff <branch_name>

Config

Configures your username and email for GitHub

Configure your user email

git config user.email “<github_email_address>”

Configure your user name

git config user.name “<github_username>”

Configure a username globally so it can be used by all repos

git config --global user.name “<github_username>”

Configure an email globally

git config --global user.email “<github_email_address>”

 

Additional Commands

  • Un initialize a git repo

    rm -rf .git
  • Unstage a file

    git reset HEAD <filename>
  • Verify your remote repo URL

    git remote -v
  • Force a push in case the push command rejects or refuses to upload your files to GitHub:

    git push origin main --force

    or force the push using the f flag

    git push -u origin main -f
  • Add, commit and push in one single command

    git add . && git commit -m "<meaningful_commit_message" && git push

    or

    git add . ; git commit -m “<meaningful_commit_message”; git push
  • Update a recently pushed commit message, use

    git commit --amend
  • Remove a file from git so it's no longer tracked:

    First add the file to .gitignore

    Remove the file from index:

    git rm --cached <file_name>

    Add, commit and push the changes:

    git add <file_name> && git commit -m “Stopped tracking <file_name>” && git push

    Verify the ignore status:

    git check-ignore -v <file_name>

    If successful, you'll get a status of 23 or 1.

Branching and Merging

Branching

By default, a git repo has one default branch which can be called master or main though this is just a naming convention.

So branching comes in when we want to add another repo, say for some new feature that we will later merge with the default branch (master or main).

Each branch in this case will have no idea what commits we have made in any other branch. Each branch keeps track of changes in it.

This is useful when we are making changes that might break our code if they are not complete especially when working with many developers. So we can work on the changes in a different branch to ensure the code in the default branch does not break.

To view all your branches, use

git branch

the branch preceded with an asterisk (*) is the one you're currently in.

To switch between branches, use

git checkout <branch_name>

To create a new branch, add the flag -b

git checkout -b <branch_name>

after creating the new branch, git automatically switches to the new branch.

To delete a branch locally, use

git branch -D <branch_name>

Merging

once we have completed the changes we were making on our branch, we can now merge our branch to the default branch using merge.

before merging we might want to see the code we are merging just to double check. We use

git diff branch_name

then to merge

git merge branch_name

README File

A README file communicates important information about your project.

A README, along with a repository license, citation file, contribution guidelines and a code of conduct, communicates expectations for your project and helps you manage contributions.

It's often the first time a visitor sees when visiting your repository.

README files typically include information on:

  • What the project does
  • Why the project is useful
  • How users can get started with the project
  • Where users can get help with your project
  • Who maintains and contributes to the project

If a repository contains more than one README file, then the file shown is chosen from locations in the following order: the .github directory, then the repository's root directory, and finally the docs directory.

If you add a README file to the root of a public repository with the same name as your username, that README will automatically appear on your profile page. You can edit your profile README with GitHub Flavored Markdown to create a personalized section on your profile. For more information, see "Managing your profile README."

A readme file usually has the file extension .md which stands for markdown.

Markdown is simply a way to format your text in files like the readme file.

It has some shortcuts like one # which represent a heading.