Intro to Git
To install Git, run
pacman -S git in an MSYS2 terminal.
- GitHub - mirrors of the code we've written for the robotics team reside here.
- Git's online documentation.
- Git lecture slides.
master is considered the stable branch which contains
tested, working code. Work on features and bugfixes should occur on
separate branches. Users should make as many commits on experimental
branches as they feel is necessary. Before a branch is merged with
master, intermediate commits are typically squashed to produce
one or several feature, bugfix, or cleanup commits.
10 Most Commonly Used Git Commands
1. Cloning a remote repository
git clone <repository> [<directory>]
This downloads the repository at the given URL to the current directory.
<directory> overrides the name of the repository's
2. Adding files to the staging area
git add <file>
This adds new, untracked files as well as changes to already tracked ones.
git add Main.cpp
3. Removing files from the staging area
git rm [--cached] <file>
This deletes and stops files from being tracked. If the
--cached option is provided, it only removes the specified
files' changes from the staging area.
git rm UnneededFile.hpp
git rm --cached Main.cpp
4. Creating a commit in a local repository
git commit [-m "commit message"]
This commits changes currently in the staging area. If
commit is called without the
-m option, an editor will
be opened for entering a commit message.
git commit -m "Adding new feature x"
5. Pushing commits to a remote repository
git push [<branchname>]
This pushes commits from a branch to the corresponding branch on a
remote repository. If
<branchname> isn't specified, Git
will use the currently checked out branch.
git push my-branch
6. Pulling commits from a remote repository
git pull --rebase [<refspec>]
This pulls the ref
<refspec> (a branch, commmit, or
tag) from the remote repository and rebases it into the current branch. If
<refspec> isn't specified, Git will attempt to pull
changes from a remote branch with the same name as the currently checked
git pull origin/master
7. Creating a branch
git branch <branchname>
This creates a new branch starting at the current commit. Omitting
<branchname> shows a list of all branches and an
asterisk beside the current one.
git branch new-feature
8. Deleting a branch
git branch <branchname>
This deletes the branch
<branchname>. This command
won't work if that branch is currently checked out.
git branch -d merged-feature
9. Checking out a branch
git checkout <branchname>
This checks out
<branchname>. The default branch when
a new project is created is called
git checkout other-branch
10. Merging a branch
git merge <branchname>
This merges commits from the specified branch into the branch currently checked out. In some cases, one may want to delete the branch after merging it (see 8).
git merge new-feature