Instruction: Git

Introduction

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.1 For detailed documentation on the software visit git's documentation page.

This guide will detail common steps and work patterns associated with the use of git. The installation of git and basic use of a repository manager such as Github, GitLab or Gitea. This guide will not go into detail of continuous integration & continuous development.

Installation

Visit the git download page for the desired operating system and follow the download instructions provided. Note, while an attempt is made to provide the most up-to-date information, links may over time may lead to non-existant pages.

Implementation

Clone

When starting a project which uses Git, the first task to complete is to start a repository locally.  Navigate in the Git Bash to the location of where you would like to clone/create a repository. 

cd <directory>

Once the git bash is open, clone the repository which is going to be developed. Enter authentication information if required.

git clone <repo-url>

Next, navigate into the cloned repository folder.

cd <repo>

And, create a new local branch. Branches can also be deleted locally by using the -d flag.

git branch <branch-name>

Checkout and use a newly created branch. One may also skip the process of both running git branch and git checkout by simply using the -b flag with the checkout command. By using this flag, users will create and checkout a branch with one command. 

git checkout <branch-name>

Add, Commit, Push

Now that the repository and branch are properly set up, add or remove files. After making the required changes, add the files to the branch in preparation for a commit. The below will add all changes to the branch. Files may be specified instead of adding all. 

git add -all

Add specific files to a branch.

git add <file-path>

Check on the status of the files to be added. This same command can be used prior to add to see which files have been modified, deleted, or are excluded from the normal file flow in the .gitignore file.

git status

If the proper files defined by the user are returned, create a commit to the local branch with a comment detailing the changes. Comments should be kept to less that 50 characters.

git commit -m "<description-of-changes>"

Push the changes to the remote repository.

git push --set-upstream origin <branch-name>

Merge

There may be occurrences in which a branch is under local development may be out-of-date and need the most up-to-date code added to it. The proper way to do this would be to navigate to the "master" or "main" branch locally,

git checkout <master-branch>

update the "master" or "main" branch,

git pull

switch back to the development branch,

git checkout <development-branch>

and merge the changes.

git merge <master-branch>

By accomplishing this pattern one can assure that the code within their local development branch will contain the most up-to-date code in development, quality assurance, or production.

Other

Outside of the simple and common workflow there may be other tasks to accomplish with Git. Such tasks include checking the differences between two files, undoing an add or commit, stashing changes. 

Restore

Developers may find it useful to restore all or specific files within their branch to what the content what pre-modification. Instead of a file path use a period (".") to restore the whole branch to the state it was prior to any modifications.

git restore <file-path>
Diff

Check the differences between two files. This is to be done prior to a commit. To exit the shell editor run command :q.

git diff <file-path>
Reset

Undo the addition of a file to uncommitted changes.

git reset <file-path>

Undo the addition of all files to the list of uncommitted changes. The flag HEAD~ can also be added to undo a commit.  

git reset
Stash/Apply

At times, when changing branches while the current branch has uncommitted changes, one may see a message from the console to stash or commit changes. While it may be simpler to commit the changes and return back to the branch at a later time, one may also stash the changes.

git stash

Once having returned to the branch, to undo the stash run the command.

git apply

 The firm has found, however, committing the changes is the simplest method of saving changes and returning back without losing unwanted file modifications.  

Fetch

In most cases, pull satisfies the requirements of developers, however, there are some unique cases where the command git fetch may be used. Git fetch will pull remote changes into a local repository but will not merge them. Once having run git fetch the git merge command must be used in order to merge the fetched changes. The command git fetch is useful for those seeking to track remote changes, but not merge them into their local branch.

git fetch
git merge

Flags

There are many common flags which users may also incorporate into their bash commands to facilitate ease of development. These flags include:

  • -m : Allows for the addition of comments within the bash command instead of a separate text file opening when running the same command.
  • --force : Ignores any warnings present by a command within the bash and completes the associated command.
  • -c : Reuse the same comment with the current command as was used with the prior.
  • -A : Is used synonymously with the "--add" command.
  • -d : Delete a local branch 

  1. Git. (n.d.). https://git-scm.com/