Git Basics

Initial Setup

Installation

Download and install Git onto your computer. What I did was that I installed it using the default settings provided with the installer.

Setup SSH keys with GitHub

With SSH keys set up, repositories can be accessed easily without the use of passwords. Follow the guide here to setup SSH Keys. I prefer using SSH Keys as I find that it works better for me.

Usage

git clone

This command when used in the Git Bash to make a copy of the repository from GitHub into your local file system.

In Git Bash:

git clone URL

where URL refers to the SSH URL of your repository in GitHub.

To get the URL, go into the GitHub repository to be cloned, and look for this:

SSHURL

Ensure that the drop down selection is set to SSH, and click on the clipboard button on the right side to copy the full URL. Paste the URL after ‘git clone ‘ and press enter. With SSH Key setup properly, you should then see that your repository is being downloaded and copied into your computer.

Once the files have been downloaded, you may then begin to make changes to your local copy of the repository, without affecting the original on GitHub.

git pull

This command is actually a convenient command for performing both the git fetch and the git merge commands at one go.

In Git Bash:

git pull origin master

where origin master refers to the master (or main/default)  remote branch on GitHub.

If a local copy of the branch already exists on the computer, this command should be used to update the local branch before further changes are made for the day. Usually this is run at the start of the work day to get the latest version of the code base to work with in a collaborative environment.

git status

This command is used to check the current status of the local branch compared to the latest known status of the remote branch (based on the time of the last fetch command).

In Git Bash:

git status

When this command has been run, multiple lines will be displayed in the Git Bash console, indicating the status of the files in the local repository. This command is useful when changes are to be committed to the local repository, and then later updated to the remote repository on GitHub. It shows the files that have been created, modified, or deleted. It also shows which files have been staged for commit. Only those files that have been staged will be committed to the local repository when the commit command is run. To stage a file for commit, run the add command.

git add

This command is used to stage files with changes for commit.

In Git Bash:

git add [option]

where option could be one of the following, a specific file, a directory, or all.

file:

git add fileName.txt

Doing this would add only that particular file to the stage for the next commit. This is useful if one component is edited in the project, and the others should not be changed at all.

directory:

git add src/

This adds all the changed files in that directory to the stage for the next commit.

all:

git add – – all

or alternatively:

git add – A

This option allows for a quick method to stage all changes for commit.

Once this command has completed execution, running git status will show that all files are staged for the next commit.

git commit

This command commits the file to current working branch.

In Git Bash:

git commit -m “Commit Message”

where -m indicates that a commit message will be specified for this commit, followed by the message itself in double quotes.

A commit can be seen as a save function. It saves all the changes made to the local branch on the computer, tagging the commit message so that future references can be made for the changes made in that branch. After running this operation, git status should show that there are no changes to be committed.

git push

This command sends the commits on the local branch to the remote branch.

In Git Bash:

git push origin master

where origin master refers to the master (or main/default)  remote branch on GitHub. master here can be replaced with the name of current working branch instead.

When this command is executed, the changes on the local branch will be updated to the branch on GitHub. If the working branch is a newly created branch on the local machine, a branch with the identical branch name will be created on GitHub and the changes pushed there. Note that if the changes is not pushed onto the master branch, a pull request has to be made on GitHub, and then changes merged to the master branch.

Leave a comment