Introduction To Version Control With Git

1. Version Control

Version control is the practice of tracking changes to any kind of data like code, documents, or artwork. Version control systems provide a complete change history and allow versions of the same data to be manipulated separately.

2. Git

The entire change history is kept in git repositories. Git repositories can be manipulated on your personal computer or shared with others on websites like GitLab.

3. Git Basics

3.1. Initializing the Repository

Setting up repository, git init. Ensure git is installed. How to install Git.

git -v

The repository is .git. To initialize the repository do:

git init

3.2. Version Control With Git

Changes must be staged and committed in the same directory as the repository to be tracked by git. The contents of the next commit are determined by what changes are staged. git commit creates a new snapshot of the staged changed.

01 gitAddAndCommit

3.3. git add

The .git file stages changes. The next commit is described by the staged changes. git add stages the latest changes of whatever file it is targeting. s

To stage the latest changes of SuperImportantPaper.txt do:
git add SuperImportantPaper.txt
To stage the latest changes to all files in the present working directory do:
git add .

3.4. git commit

git commit appends a new snapshot, specified by the staged changes, to the git repository.

To track information about the commit author, git needs and email and name.

To specify the author of the commit’s email and name do:
git config user.email "AllFather@Highlands.Above"
git config user.name "YourAllFather"

You need to specify the email and name for commits to the repository, otherwise the following error occurs when running git commit.

$ git commit
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address
To commit the set of staged changes do
git commit

Enter a commit message with the default text editor set for git commit.

02 gitCommitEditor

3.5. Branches

A branch is a independent series of commits. Each repository begins with a main /master branch. Branching can be used to keep a separate series of commits from the main to facilitate multiple people working on the same set of data.

To see the current branches in the repository do:
$ git branch -v

# A fancy way to see the branches with git log. Press q to quit

$ alias.lgb=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches
$ git lgb
03 gitBranchAndMerge

3.6. git branch

git branch starts a separate branch from the current branch. Changes to this branch won’t be tracked in other branches.

To start a new branch from the current snapshot do:
$ git branch differentBranch
To see all branches do:
$ git branch -v

3.7. git checkout

git checkout switches from the current branch to a different branch.

To make changes to a branch you must check it out first. To check out a branch do:
$ git checkout differentBranch

3.8. git merge

git merge combines the current working branch changes with another branch’s changes. The other branch is merged into the current branch by using git branch.

To merge a different branch with the current branch do:
$ git merge differentBranch

4. Hosted Git Basics

  • GitHub and GitLab are online platforms used to collaborate and **

  • store repositories. ** To edit a repository stored on GitHub or GitLab it must be cloned. GitHub and GitLab provide forking capabilities which are similar to branching.

4.1. Clone A Repository

git clone allows editing of a repository on GitLab. git clone downloads the repository from GitLab into the current working directory.

To clone a repository navigate to it’s page on GitLab and copy it’s clone link:

04 gitCloneLink
Next use git clone with the link as follows:
$ git clone git@gitlab.trecis.cloud:nbc170001/docs-science-support-meeting-minutes.git

4.2. Push To A Repository

git push allows users to make changes to the repository stored on GitLab. git push uploads the local snapshot of the repository to the online repository.

To push yourBranch to the online repository do:
$ git push origin yourBranch

4.3. Fork and Pull A Repository

Forking allows users to make their own copy of another person’s repository on GitLab which they can freely edit.

To fork a repository navigate to the repository on GitLab and select fork:

05 gitFork

To make changes to your fork, clone the repository and push your changes.

Pulling is how changes are made to a GitLab repository based on it’s fork.

To submit a pull request navigate to the original repository’s GitLab page and do compare and pull request button:

06 gitPull