Git
Git is a VCS (Version Control System) that operates by calculating diffs (differeneces between files) between two versions of a project.
To begin using git
, install the git
command line tool. Git is often
preinstalled on most linux distributions, and OSX, for windows or exceptions,
see instructions at git-scm.org.
To begin using Netsoc-as-Code, you must first add an SSH key to the setup-control-host.yml file. Instructions on how to generate an SSH key and can be found here, and instructions on how to add the public key to Github can be found here.
Git Basic Training
To create a project with Git, you must first create a directory, and initialize it as a git repository.
$ mkdir <project>
$ cd <project>
$ git init
If you have created a repository on Github, you can set the origin to the Github repository.
$ git remote add origin git@github.com:<your username>/<your project>.git
Alternatively, to begin working on a pre-existing project, we first have to
clone
the project. As you should have already set up an SSH key, you can
use SSH to authenticate yourself to Github.
$ git clone git@github.com:<org or user>/<project name>.git
To commit
(save) changes, you must first add changed files to the staging
area.
$ git add <file or directory name>
and you can use git status
to see what files are staged for commit.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes staged for commit:
modified: newfile.txt
It is a good idea to use
git status
to make sure you've added all of the needed files, but also to ensure you don't commit something you shouldn't like a secret file (like a .env).
Now, finally to commit changes staged:
$ git commit -m "<message briefly explaining changes>"
And to push to the remote repository:
$ git push origin <branch name>
How we develop code
We use git for every project we develop. A feature of Github that we take advantage of is Pull Requests. Pull Requests allow us to merge changes from one version of a project into another. This allows us to branch off from the 'main' branch and work on a new feature, before creating a Pull Request to merge the new feature into the main branch.
Git Branching
The git checkout
command allows us to switch between and create new branches.
To go to <branch name>
git checkout <branch name>
To create and go to a new branch
git checkout -b <new branch name>
After pushing commits and finishing work on a branch, you can open a Pull Request on Github to merge in your changes, by going to the 'Pull Requests' tab on the repository page on Github and selecting your branch.