Git Basics and Tricks - ArgyleData/docs GitHub Wiki
There are many documents written on Git. These are just my notes based on the questions about Git voiced here in Argyle Data. While RTFM is always a sound advice, I tried to answer as many questions as I could here. IR
Git - a modern Source Control System. Here are features of Git that are important to understand for the purpose of this article.
- Git repository contains all the code of the project including all its history
- Git is a graph where nodes are commit points.
- Every commit point (except for original genesis) has its parent point
- A branch is a sequence of commits. The primary branch (called master) is created automatically
- Every new branch is just a new commit. When a branch B is created as a subbranch of A, it is just a new commit. At the time the branch is created, we have 2 different points to the identical code base. However commits for branch A and commits for branch B will have different predecessors, and development in different branches would be maintained simultaneously.
- Size of repository depends on number of commits, not on number of branches. Each branch is a trivial commit without source changes.
- When a project is supposed to be used as a starting point of another project, it can be forked. Fork creates a separate repository.
To use a git on local machine one needs to install Git. Once Git is installed, there are quite a few things that help make Git experience reliable and convenient.
For Git on Mac OS please follow the link https://gist.github.com/trey/2722934. This procedure requires Homebrew installer for Mac - if you don't have it installed, you could find instructions here.
For Git on Linux here is probably one of the best references - https://wiki.archlinux.org/index.php/Git.
A local Git environment can be created in any directory. A command
git init
effectively create all necessary structures in the working directory, and all changes in the directory and subdirectory will be immediately traced.
A remote repository is created on Github. Normally developers do not deal with creation of remote repositories, so this process we will not discuss here.
Normally a developer just clones a remote repository into a local repository, then works mostly in local repository, and periodically pushing the changes back to the remote repository. Here is a sample of cloning command:
git clone [email protected]:ArgyleData/extract.git
This is a command to clone Extractor code into a local repository.
A git repository is basically made out of few basic parts
- files - source files kept in the repository
- commits - stored update actions containing deltas (source changes) and some metadata
- branches - special markers of certain kind
- labels - special markers of another kind
When a repository gets created it has an initial state and a base branch master.
To check which branch you are in, issue command
git branch
It will show you all the branches and highlight the current branch. If a Git prompt has been installed, then the shell prompt will always tell you what branch you are in.
After some changes have been made, following command shows all the pending changes.
git status
Commands add, rm, give additional control over the changes to be committed. Eventually a following command creates a new commit:
git commit -m "comment - what exactly is contained in this commit"
A commit is an entity with an opaque name that represents a certain change to the codebase. The following command shows commits within a branch:
git log
Here is a sample of the output of git log command:
commit d14efe84eae073d447efa337307a797e3f52a74c
Author: irvingrabin <[email protected]>
Date: Wed Sep 9 11:52:09 2015 -0700
changing abc.txt in develop
commit 0c3036c3cbfd920000deed3ad9fd5f8521460474
Author: irvingrabin <[email protected]>
Date: Wed Sep 9 11:45:38 2015 -0700
fixing abc.txt
commit 06c264654f289cce6187fbbabfaf7b6f88e6b215
Author: irvingrabin <[email protected]>
Date: Wed Sep 9 11:44:21 2015 -0700
Adding first file
Each commit belongs to a certain branch (originally master). Creation of a branch is just a special kind of commit. It also has its opaque id, which originally points to exactly the same code base but with different branch name.
To create a new branch use a command
git checkout -b <branch>
Each branch has so called HEAD, the latest commit on the branch. From now on a following command
git checkout <branch>
will switch to the HEAD of different branch. Each branch is (logically) a sequence of commits starting with branch initiation commit. Commits in a sub-branch are separate from commits in parent branch.
NOTE. There is a misconception that cloning a repository with multiple branches would involve storing multiple copies of the codebase. Actually we store commits only, and since creation of the branch is just another commit, the size of the repository reflects only amount of changes made by developers regardless of what branches they were working in.
Once a development in a subbranch has been completed and ready for merging back into a parent branch, there are 2 main options:
- if the same developer owns both branches, then [s]he merges the branches
- if the sub-branch developer does not own parent branch, [s]he sends a pull request to the parent branch owner requesting pulling the sub-branch changes to the parent branch.