Home Git Basics
Post
Cancel

Git Basics

What is Git?

Git is a Source Code Management (SCM) or version control tool. It is usually used for coordinating work among several programmers for developing software. To learn lots of neat things Git can do check out the reference material here: git - Documentation

Installing Git

Git comes installed on most *nix based systems. I can’t think of any major distro that doesn’t have it by default. Maybe Biebian, I don’t know, never tried it.

For Windows you can grab the installer here: https://git-scm.com/download/win.

Setup Git

Git needs to be setup before you can use it. This is used to track who made changes.

1
2
git config --global user.name "GlobalUser"
git config --global user.email "[email protected]"

If you want to use specific information for each project you can use the following within each repo:

1
2
git config user.name "ProjectUser"
git config user.email "[email protected]"

Repository Terms

  • repo = repository
  • Local repo - a local repository on your computer used to work on the local version of a project.
  • Remote repo - a repo that is usually stored on another system or server. This is often used when multiple people want to work with a project’s code or you want to move the code to another place, like GitHub.

Initialize your local git repo

1
git init
1
2
git init
Initialized empty Git repository in D:/Dropbox/wwtw/Live/.git/

Check the status of the new repo

1
git status
1
2
3
4
5
6
7
8
git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
          index.html

Tracking and Commiting

Tracking With Git

Git has two step process for managing files. First you add files to staging. This allows git to track them. Then you commit them. Commiting is recording the changes to the repo.

A single file to staging

1
git add index.html

Multiple specific files

1
git add index.html index2.html

All files in directory

1
git add .

All folders and files in this directory

1
git add --all

All folders and files of a specific directory

1
git add subdir

Git Commit

Here we commit the changes we tracked above and add a message with the action.

1
git commit -m "Created repo"

Commit History

This command will give you the commit history messages and the “hash” of the commit.

1
git log

Branches

Git works like a timeline, think Back to the Future II. Git calls these branches. By default, in a new repo, you are placed in the branch master. You can see this via git status.

1
2
git status
On branch master

Or with git branch, this will show you the branches and the one with the star will show you what branch you’re on.

1
git branch

To split your project to a new branch at the current point use git branch branch-name. This is useful if you want to test some changes and play around without impacting the main code.

1
git branch <branch-name>

Rolling back

One of the greatest features of git is the ability to roll back to a previous version of your code.

1
git checkout <commit-hash>

To go back to the latest version of your code use

1
git checkout master

To rollback your code to a previous version and make changes you need to split the changes to a new branch. This can be done with -b. This is helpful if you want to experiment with a bug fix but not change the code for everyone.

1
2
3
git checkout <commit-hash>
(make changes)
git checkout -b <new-branch-name>

You can swap to another branch any time with

1
git checkout <branch-name>

Merging branches

If you like the changes and wish to combine one branch with another use git merge branch-name. To use this command change to the branch you wish to merge the changes into. For example, if you want to update master with the changes in test-branch-name use the following:

1
2
git checkout master
git merge test-branch-name

This moves us from whatever branch we were in to master. Then we merge the changes with test-branch-name into the branch we’re in, master.

Putting this all together in an example

1
2
3
4
5
git branch 1955-biff
git checkout 1955-biff
git merge 1955-biff
git checkout master
git merge 1955-original
  • I create 1955-biff for when he travels back in time
  • Then check it out to use it an make changes
  • Merge the 1955 Biff created with the original 1955 overwriting the original
  • I then checkout master
  • Merge the 1955 changes with master so everyone is now using Biff’s self created future

Enjoy your versions of things.

Updated Website Theme

Deploy a Project to Your Server with Git

Comments powered by Disqus.