GIT Version Control
Git is a commonly used version control tool for managing software source code files. Git is capable of managing any file type including binary files. It's efficient in terms of storage as it only tracks the deltas for modified files. A Git history (stored in ".git" directories) contains all previous modification states and allows full history tracking and recovery.
Github is the most common online Git repository service and is free to access. Web based repositories and known as "remote" repositories in Git terminology. There are GUI based Git tools and tools integrated into IDEs that provide easy access to beginners. For example VSCode contains Git functionality by default. For people who prefer command line functionality Git has you covered. I personally prefer to use Git via the command-line interface (CLI).
The following command-line instructions are the mostly used Git commands that you should get familiar with. Even if you prefer to use a GUI application it's beneficial to learn these as it's a great way to become familiar with the Git workflow and terminology.
Configuration
The Git config tool can be opened by typing git config
at the command-line.
To view the config use git config --list
. The following are some useful commands.
Setting username and email:
git config --global user.name "My Name"
git config --global user.email myname@mydomain.com
Clone a Repository
To clone an existing repository from Github use the clone
command. This will copy
the repository into the current directory under a new directory with the same name as the repository name.
git clone https://github.com/myname/myrepo.git
Create New Local Repository
git init
To initialise a repository based from the current directory and create a .git directory.
Adding Files to a Commit
A "commit" in Git terminology refers to a collection of file altertations. When creating a new repository you would most likely create a commit containing all the relevant files. When modifying an existing an existing repository you would create individual commits for each related set of changes.
git add *
This will add all files and all file modifications in the current directory to the next
Git commit A .gitignore file should can be used to specify which files or file types you do not wish to add.
If you only wish to commit individual files or individual modifications you can specify filenames:
git add <filenames>
A handy feature is to use the git add command with the -p flag which will prompt you for every alteration whether or not to add the
alteration to the next commit. This is useful if you have made multiple alterations and wish to add them to more than one commit.
See the breakdown of options here.
git add -p
After adding files or modifications to a commit you can create the commit with the git commit
command. The -a flag
is used when you wish to add all tracked file modifications to the commit. It can be omitted if you have already added modifications
using the git add command. The -m flag stands for "message" and allows you to add a description for the commit.
git commit -am "First commit"
When you are ready to upload your location repository to Github you will need to add a "remote". This tells Git the location of the remote repository.
git remote add origin https://github.com/myname/myrepo.git
If you wish to remove a remote location you can use: git remote rm origin
Update Local to Match Remote
A git pull command will fetch data from the remote location (eg.Github) and merge it with the local files. It's a
good idea to do a pull request before working on some files, to make sure your base files match remote. This is
especially important if working from multiple PCs. git pull <remote>
Update Remote to Match Local
Now the moment all the hard work has lead to! To "push" you files and changes to the remote location use git push
.
The "main" location specified here is the branch name. Git will use main as the default however it's possible to rename it. It's
also possible to add multiple branches when adding new features to your application.
git push -u origin main
Restore Local files
The git checkout
command can be used to restore local files to match the Master branch.
This is useful if a local file is unintentionally deleted or modified and you do not wish to commit these changes.
For this command to work the changes must not be staged/committed. You can use git status
to check if the changes are staged. git checkout -- <file(s)>