Getting started with Git


Let's begin

git init — creates repository.

git init newproject — creates and names repository.

git add — add file for git to track.

git commit -a -m "note here" — commit all changes and add commit note.

git status — prints status based on what's changed since you last committed.

touch file1 file2 file3 — add files (without overwriting previous). Added files before they are commited exist in the staging area

git log — prints all commits. You can check a version out of the repository the same way you would a book from a library.

git checkout xxxxx — takes you back in time to view old versions of file and we can use cat filexxx to take a look at them.

git checkout master — return to master branch, current place in time.

git dif xxxxx xxxxx — will print all that is different between 2 commits.

git checkout HEAD~1 — checkout previous commit before last.

git checkout HEAD — return to master branch.

git rm — removes file.


BRANCHING IN GIT

The master branch also referred to as trunk
is generally always the version deployed in production.

git branch xxx_feature — new branch (short and meaningful for name).

git checkout xxx_feature — switch to work on branch.

git checkout master — switch back to trunk again.

This can also be done in one command:
git checkout -b xxx_feature — whenever you branch you start from an exact copy of where ever you are.

git branch — prints all branches along with indication of which branch you're on.

git branch -D xxx_feature — delete branch — cannot delete while currently within branch

Big benefit of working with Git as a version control system is that merging can be a reasonably painless process. Git will assume and work out most conflicts where some other version control systems may ask for it to be done manually. if conflicts are on different lines they will automate merge. If not will require manual.

git merge foo_feature — merge branches/with master/trunk.


RESOLVING MERGE CONFLICT

Simplest is to edit conflicted file

conflict markers as followed:

<<<<<<< HEAD
Hi my name is Phillip.
=======
Hi my name is Toby.
>>>>>>> new_name

And then simply resolve by amending file.


REMOTE REPOSITORIES

git clone — makes a new version of whole repository.

git remote — will display origin if repository is a clone. If not and it is an original then nothing will display. In this case...

git remote add [clone_name] [clone_repository_name] — will set up and tell original to watch clone.


PUSHING AND PULLING

git push [directory to] [branch from]

git pull [directory from] [branch to] — resolve conflicts in the same way as between branches.

Add further break down looking at Staging Area

Show Comments