Version Control Using Git

In the following code snippets, you must complete replace any terms in angle brackets. (The result will not have angle brackets.) Issue all commands in your shell (e.g., PowerShell).

Clone an Existing Repositiory

So that you can contribute to it, make a local copy (i.e., on your device) of an online repository as follows.

git clone <url> <localFolderName>
<url>

the URL for the central repository you want to clone (which is typically on GitHub)

<localFolderName>

the path to the folder in which you want to create your working directory (i.e., your clone of the central repository)

Typically you first cd to the folder where you want to create the folder, in which case you may omit the second argument. (The created folder will then have the project name.)

Listing Changes

Show the state of the working directory and staging area.

git status

This lists all modified tracked files, showing whether the changes are staged or unstaged. Shows a separate list of the untracked files that are not ingored.

Preparing to Commit Changes

Git separates adding and committing changes! Adding a file puts the changed version in a staging area From the staging area, files can then be committed.

To add a changed version of a file to the staging area:

git add <filename>

If you change the file again, you need to add it again, even if you have not yet committed it.

Committing Changes (on your device)

Git looks at the staging area to determine which files should be (locally) committed. To commit all of the changes in all of the files in the staging area:

git commit -m "<change-description>"

Note that the commit message describing your changes is required and must be in quotes. Try to make it as descriptive as possible.

If you know for sure that you wish to stage and commit all current changes, you can combine these steps:

git commit -a -m "<msg>"

The commit is still local and has not yet been pushed to the central repository!

Pulling Updates from Origin and Pushing

After committing all your working-copy changes, you may want to share them by pushing them to the central repository.

First, check your current branch with the command:

git branch

This will display your current branch name with an asterisk (*) next the name. (During your first experiments with git, the branch is probably master.) Use pull to update your local branch with the current remote branch:

git pull origin <branchname>

After pulling all the remote changes, and assuming no conflicting changes, you can push your code to the remote repository. Since you have already committed your local changes, just use the command:

git push origin <branchname>