Intro to Git
To install Git, run
pacman -S git in an MSYS2 terminal. Set
up the commit author information with the following commands.
git config --global user.name "Your Name" git config --global user.email "firstname.lastname@example.org"
In general, Git tries to be helpful. If you don't know what to do after running a Git command or seeing something fail, read the text Git prints for possible hints or instructions.
- GitHub - mirrors of the code we've written for the robotics team reside here.
- Git's online documentation.
- Git lecture slides.
- The Git Parable - an alternate explanation of Git terminology.
- How and why to write a good commit message
- Flight rules for Git: solutions to common problems.
- PDF on Git internals for those curious about how Git stores files and revisions.
Git lecture notes
- We'll be covering a bit of graph theory
- Makes students better able to recover from mistakes
- You should never have to reclone to fix issues
How Git stores data
- Snapshots, not diffs
- Where is data? (.git folder)
- Giant GRAPH OF POINTS where commits are points
- POINTER to currently referenced commit
- Getting repository (clone)
- Making repository (init, [w/ --bare for servers])
- Git staging
- untracked <-> staging area -> commit
- How to mvoe between each stage?
- u -> s: add
- s -> u: rm --cached or rm
- s -> c: commit -m or commit
- How to keep certain files out of staging area? (.gitignore)
- Ex: Robot-2017/.gitignore
Getting changes from others
- Not just a server -> specify place, mention remotes
- pull (fetches changesets in remote branch and merges them)
- Most of work will be done in them
- Making a branch (branch)
- Moving onto a branch (checkout)
- Moving work onto master (merge)
- What if merge goes wrong?
- Find <<<<, ----, and >>>>
- commit result
- Deleting a branch (branch -d)
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 -d <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