Git is a (free) version control system. It allows:
- saving code snapshots (commits)
- working with alternative code versions (branches)
- moving between branches and commits (checkout)
- easily rolling back to older code snapshots or develop new features without breaking production code
Repository is a bucket which contains a code.
GitHub is a platform for creating, storing and managing Git repositories in the cloud
- allows multiple developers to work simultaneously on the centrally stored code base
- keeps the code safe in case code is lost on dev machines for any reason
- provides tools for automating code and repository management via GitHub Actions
Switching repositories
Cloning a remote repository:
$ git clone https://github.com/OpenVPN/openvpn-build.git
After cloning desired repository it is always good to run the following command and fetch/clone all its submodules:
$ git submodule update --init
Cloning a remote repository with preserving UNIX-style line endings:
$ git clone https://github.com/OpenVPN/openvpn-build.git --config core.autocrlf=false
Working with Branches
Getting a list of all local branches:
$ git branch
Deleting local branch:
$ git branch -d my_branch
Deleted branch my_branch (was 6d5921868b).
Getting a list of all remote branches:
$ git branch -r
Getting a list of all branches at some specific path (on Windows):
> git branch -r | find "origin/private/bojan"
Example output:
origin/HEAD -> origin/master
origin/master
origin/release/2.3
Deleting remote branch:
git push origin --delete my_branch
To update local list of remote branches:
$ git remote update origin --prune
To list all local and remote branches:
$ git branch -a
Checking out the remote branch (for the first time):
$ git checkout -b branch_name remote_name/branch_name
or shorter (on newer Git versions):
$ git checkout --track remote_name/branch_name
Example:
$ git checkout -b release/2.3 origin/release/2.3
Branch release/2.3 set up to track remote branch release/2.3 from origin.
Switched to a new branch 'release/2.3'
Or:
$ git fetch
...
* [new branch] ModuleA/Project2/JIRA-123 -> origin/ModuleA/Project2/JIRA-123
...
$ git checkout ModuleA/Project2/JIRA-123
Checking out files: 100% (1485/1485), done.
Branch ModuleA/Project2/JIRA-123 set up to track remote branch ModuleA/Project2/JIRA-123 from origin.
Switched to a new branch 'ModuleA/Project2/JIRA-123'
If you get the following error:
$ git checkout ModuleA/Project2/JIRA-123
error: Your local changes to the following files would be overwritten by checkout:
Module2/SomeDir/SomeFile.cpp
Please commit your changes or stash them before you switch branches.
Aborting
...you can force checking out with -f switch:
$ git checkout -f ModuleA/Project2/JIRA-123
To create new local branch:
$ git checkout -b issue-007
Switched to a new branch "issue-007"
The command above is a compact version for a group of these two subsequent git commands:
$ git branch issue-007
$ git checkout issue-007
To create a new branch based on a commit from the current branch:
$ git branch <commit_sha1>
To create a new branch based on a commit from another branch:
$ git branch branch_name <commit_sha1>
To create that branch and check it out:
$ git checkout -b branch_name <commit_sha1>


