Git is a widely used version control system that allows us to create repositories and collaborate on projects. This guide will serve as a quick reference to the commonly used tools and commands of Git. For more in-depth documentation, please visit here.
This guide will cover how to:
At the end is a useful table of essential Git commands.
A repository is usually used to organize a single project. Repositories can contain folders and files, images, videos, spreadsheets, and data sets – anything your project needs.
To create a new repository:
Branching is the way to work on different versions of a repository at one time.
By default your repository has one branch named master which is considered to be the definitive branch. We use branches to experiment and make edits before committing them to master.
When you create a branch off the master branch, you’re making a copy, or snapshot, of master as it was at that point in time. If someone else made changes to the master branch while you were working on your branch, you could pull in those updates.
To create a new branch:
On GitHub, saved changes are called commits. Each commit has an associated commit message, which is a description explaining why a particular change was made. Commit messages capture the history of your changes, so other contributors can understand what you’ve done and why.
Make and commit changes:
These changes will be made on your current branch. If you are on a branch other than master, this branch now contains content that’s different from master.
Since you will all be working in teams, the best way to collaborate on a project is to pull, or clone, the project repository from Git, make changes remotely, commit those changes, and push the new changes to the project repository.
To clone a repository:
To commit changes you’ve made remotely:
Pull Requests are the heart of collaboration on GitHub. When you open a pull request, you’re proposing your changes and requesting that someone review and pull in your contribution and merge them into their branch. Pull requests show diffs, or differences, of the content from both branches. The changes, additions, and subtractions are shown in green and red.
As soon as you make a commit, you can open a pull request and start a discussion, even before the code is finished.
To open a pull request:
|git clone URL-or-path-to-repo||Clone a repository. This makes a new folder in the current directory containing the files in the repository in which you can work on the code.|
|git add||Add the given file to the repository so that it will be tracked by git. Use this when you author a new code file and want to include it in a commit. Alternatively, include -a to add all files from your local repo.|
|git commit||Finalize the current changes to your code as a commit to your current branch and repo on your local machine.Include -a to automatically add changed files that git is already tracking and -m [message] to include a message about the commit (otherwise you will be kicked to an editor in which to type out your message).|
|git pull||Pull any changes that have happened on the remote server you initially cloned and bring them into your local repository. You will need to have a clean repo so you will probably want to commit first if you have changes (or stash them). (NOTE: a git pull is a combination of a git fetch and a git merge which will lead to extra commits of the form Merge branch 'master' of .... For a cleaner history, use git pull --rebase)|
|git push||Push whatever commits you have made locally to the remote repository that you cloned previously. You may need to pull first to ensure that you are in sync with the repo.|
|git log||View history of commits that you have made. They will be displayed with their unique identifier (a big hex mess like 766f98f32fa...) and their commit message.|
|git checkout -b [branch]||The git checkout command lets you navigate between the branches created by git branch. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.|
|git fetch||When you git fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files.|
|git merge||To integrate the commits into your master branch, you use git merge.|
|git diff||View the difference between the current code and the latest commit. You can also enter two commit IDs to see the difference between the code at each of those commits.|
|git reset [ID]||Reset the state of your repo to that of a specific commit from the past.|
|git stash||Stash away the latest uncommitted changes. Later, you can run git stash apply to restore these changes.|