Skip to content

Git github notes

Introduction

What is Git?

Git is a distributed version control system. It's a system that records changes to our files over time and we can recall specific versions of those files at any given time. Many people can easily collaborate on a project using Git.

Git allows many users to have their own version of the project files on their computer to work on and collaborators can "merge" their changes to the rest of the collaborators.

Git stores revisions in a project history inside your project directory. You can rewind to any previous version in the project. You can work on new features without messing up the main codebase.

GitHub is an online service that hosts your project files. It allows you to share your code with other collaborators.

Installing Git

The first way is to go to https://git-scm.com/downloads and download the one for your operating system.

Second way is by installing Homebrew first, then Git.

Then you need to setup the name and email address for the user. - git config --global user.name <your_name> - git config --global user.email <yourname@domain.com>

How Git Works

A repository is a container for a project you want to track with Git. You can have many different repos for many different projects on your computer. Like a project folder which Git tracks the content for us.

When you choose to have your project be tracked by git, there will be a .git file created in the root project folder.

"Commits" are save points along the growth of the project. You create commits at small, incremental times. If we want to go back to a previous commit, we can do that.

There are three stages along the wait to creating a commit: 1- make changes to the files 2- add them to the staging area 3- commit the changes to the branch that we are on

Creating a Repository

In terminal, navigate to the directory you want to create your new repository. Then, type git init inside this repository. This will create the .git directory.

staging Files

In order to stage files, in terminal, type git add . or git add filename or git add *.

If we now type git status we will see the files that that are staged in green and the ones that arenot staged in red.

To un-stage a file, type in git rm --cached filename. If we have many files to un-stage, or we want to un-stage all the staged files, we type git rm --cached filename to do this.

Making Commits

To commit the files, we need to write a commit message which will describe the what and why of the changes.

Type to following in terminal to commit the staged files: git commit -m "<message>"

Each commit will have a commit ID. Once committed, the terminal return the commit ID as well as the branch it was on and the number of files added/deleted and insertions that were made.

To view the commits for a project, type git log in terminal. To get out of the log, type "q". To view a condensed version of the logs, type git log --oneline in terminal.

Undoing Commits

There are three methods to undo some commits and go back to a previous version of our project: 1- checkout commit 2- revert commit 3- reset commit

These are in order of danger. Reset being the most permanent of the three.

Checkout commit: this will show us the code from a previous commit in our text editor. The reversion back to this older commit is in "read-only" and will not save over your most current work.

To check out a previous commit, type in git checkout commitID in terminal. The, to go back to the current state, type in git checkout master in terminal (or whatever branch you're on).

revert commit: this will remove a specific commit from the code. It lets us undo a specific commit.

To revert to a previous commit, type in git revert commitID in terminal. This will open up a text editor (in terminal). Simply exit the text editor to finish the revert.

If you take a look at the log at this point, you will see that we actually added a new commit and it will be called Revert "commit message from reverted commit"

Reset commit: this will permanently take you back in time to a previous commit. Any newer changes will be gone permanently.

To reset to a previous commit, type in git reset commitID in terminal. We can also type in the following to remove any changes that are still present and saved on our local machine: git reset commitID --hard

Branches

The main branch is the "master" branch. It represents the stable version of your code, the one that is released to the public.

Branches can be used to write and test out new features without affecting the master branch. Once we are happy with the new features and we want to implement them into master branch, we need to "merge" the branches together (back to the master branch).

You can have multiple people working on their own branch at the same time and merge them together at a later point down the line.

To add a branch, type in git branch <branch_name> in terminal. To view the branches, type in git branch in terminal. This will show us all the branches for this project. We can then type git checkout <branch_name> in terminal to switch to the new branch. Alternatively, you can type git branch -b <branch_name> in terminal to create the branch and switch to it.

To delete a branch, switch back to the master branch first, then type in git branch -D <branch_name> in terminal. This only works for branches that have not been merged. A lowercase "d" can be used for deleting branches that have been merged.

Merging Branches and Conflicts

To merge a branch back to the master branch, git merge <branch_name> in terminal.

If there is a conflict upon merging, git will let you know which file it's in. You will also see the issue inside the file in question. Git will ask us to fix the conflict and to commit the results. Once the issue is fixed, type git commit in terminal with no message and the merge will then be completed.

Introduction to GitHub

GitHub is a service that lets us set up hosted repositories (called a remote repository).

To sync the work you've done on your local repository to the remote repository, you need to "push" your work to the branch by typing git push origin branch_name in terminal.

If you are starting a repository on your local machine, you will then need to push it to GitHub to create the remove repository. To push the work for the first time, you will need to type git push SSH-URL master in terminal.

A second option is to create the repository remotely first, and then pull it to your local machine afterwards. One you create the repository on github.com, you need to click on "clone" and then copy the URL. Then, in terminal, navigate to the parent directory where our project directory will be and type in git clone SSH-URL. This will create a new directory with the repository name.

Collaborating on GitHub

Before starting to work on a project that we are collaborating on, it's best to type git pull origin master in terminal just to make sure we have the latest files in case someone else has worked on it.

Then, we need to start a new branch to work on the new features. Any work we do will be committed to this branch and pushed to the remote repository.

We can then create a "pull-request" to ask the others to review our branch and decide if we should merge this branch into the master.

Forking and Contributing

A fork makes a copy of the whole repository from someone else's account on your own account. you can then clone the repository to your local machine.

Once we have created features we want to add to the original project repository, we can create a "pull-request" to ask the others to review our fork and decide if they should merge our contributions into the original repository.