Skip to content

Instantly share code, notes, and snippets.

@yerbestpal
Last active December 27, 2019 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yerbestpal/f619f7ff6b565da92d9b6141811a3a6b to your computer and use it in GitHub Desktop.
Save yerbestpal/f619f7ff6b565da92d9b6141811a3a6b to your computer and use it in GitHub Desktop.
Notes transcribed from the Codecademy 'Learn Git' course.

Git & Github

These notes transcribed from the Codecademy 'Learn Git' course. They are for personal use to reinforce my own learning and are on Github purely for backup purposes. I do not claim ownership of any content at all provided by codecademy.

Git is a version control system used to track changes to source code and provide access to it to others, encouraging open contributions, bug reporting and testing.

Github is a website which hosts Git repositories which others can access to contribute to the project with bug reports, code contributions and testing.


Git Basics

Lifecycle

Initialise Project

The project is initialised as a Git repository using init:

git init

Add Files

One or many files can be added to the staging area using add:

git add filename1.txt filename2.txt filename3.txt

All files in a folder can be added by using a dot instead of filenames:

git add .

Commit Changes

Changes are commited to the repository with a message using commit:

git commit -m "Initial commit."

Logs

A log of commits is kept to refer back to earlier versions of a project. This can be viewed using log:

git log

The log contains, among other things, a unique identifier called an SHA. It looks something like 0050caad09188cd35dceb52fe92f20a4ccc9cf2a.


Diff

To show the differences between a file in the working directory and the version in the previous commit, use diff:

git diff

Show

You can view the details about a commit by using the show command, followed by the name or SHA, like so:

git show HEAD

It shows everything that log would, plus all the relevant changes.


HEAD

The commit you are currently on is known as the HEAD commit. The HEAD commit is often the most recent commit.


Checkout

Using checkout will restore a file in the working directory to the exact state it was in in a previous commit. So for example, if you have deleted several paragraphs in your text file, but decide you want them back, but cannot possibly remember all the words, then you could use checkout to return to a point when they were not yet deleted, like so:

git checkout HEAD page-1.txt

You can use -- in place of HEAD to speed things up:

git checkout -- page-1.txt

Reset

Reset is used unstage files so they are not commited. You might have staged the file and then realised there was an error in there that you do not want to commit, so you use reset to bring the files in the staging area in sync with whatever commit you define, such as HEAD:

git reset HEAD page-1.txt

The output from using reset will show each file with reverted changes, and the letter M to their left, denoting modification.

Reset also allows you to reset the full project to a previous commit by passing it the first 7 characters from a commits SHA, like so:

git reset 7be7ec6

git reset diagram




Branches

A git branch is a different version of the project. it contains all the commits from the Master branch but also unique commits. New branches are made for a reason, usually to introduce new features. Eventually branches are usually merged into the Master branch once they have been confirmed to funtion as expected with minimal bugs and it is agreed the changes are to be implemented.

branching diagram

New Branch

To create a new branch, use branch:

git branch branch_name

View Branches

To create all the project branches, use branch without supplying a name:

git branch

This will also indicate which branch is the active branch, with an asterix beside it and sometimes a change of colour:

git branch example


Checkout Branches

To switch between branches use checkout:

git checkout branch_name

Once you have switched to a different branch, that branch is now the active branch, and any changes you make will affect it, and not any other.

New branches also inherit the master branches commit history.

All commands that work with the master branch work on any other branch too.


Merge Branches

It is possible to merge changes from one branch into another using git merge, for example, merging the fencing branch into master:

git merge fencing

In this example, fencing is the giver branch. Master is the receiver branch. Before running merge, you must checkout the branch the receiving branch.

Fast-Forwarding

The output from the previous example would have looked like so:

branch merge example

Notice the words "fast-forward". The merge is considered a fast-forward because Git recognises that the fencing branch contains the most recent commit, and so merging with it causes master to be brought up to date, in-line with branching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment