Git Reference
Concepts and Definitions
- repository
- a project tracked by Git, consisting of commits & branches, usually stored with project files and directories in a working directory
- remote repository
- a repository that is not on your local machine; usually, it is hosted by an online provider such as GitHub or GitLab
- working directory
- aka. working tree or workspace, a directory containing a working copy of project files, where Git has been initialized to version control those files
- index (aka. cache or stage)
- staging area for building a commit of changes in the working directory
- commit history
- a database storing past commits
- commit
- a snapshot or record of changes to files in the working directory at some point in time
- branch
- a reference to a commit at the end of a chain of commits
- HEAD
- a reference to the commit that is currently checked out
- merge commit
- a special type of commit object that joins two (or more) branches
- merge conflict
- a condition that arises from a failed automatic merge; requires manual editing to resolve the conflict
Ref Notation
- HEAD
- Reference to the commit currently checked out
- ref
- in the following, this denotes a placeholder for a branch, tag, or the SHA-1 hash of a commit object
- ref^n
- the nth parent of ref, where n=1 when omitted (only merge commits have multiple parents)
- ref~n
- the nth ancestor of ref, where n=1 when omitted
- ref@{n}
- the nth reflog entry of ref
Examples
HEAD^- denotes the parent of the
HEADcommit master~3- great grandparent of the latest commit on master
HEAD~5^2HEAD’s great-great-great grandparent’s 2nd parentHEAD@{1}- previous value of
HEAD 0c708f- example of a reference to a commit by SHA-1 hash (unique ID)
First-time Setup
You must configure Git the first time before using it. Perform this one-time configuration if you just installed Git or if you already had Git installed but never used it.
The first thing you should do is set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:
git config --global user.name "Foo Bar"
git config --global user.email "foo.bar@example.com"
Next, you should generate and register your SSH key.
Run this command and answer the prompts to generate your key-pair:
ssh-keygen -t rsa
Then, run this command:
cat ~/.ssh/id_rsa.pub
Copy and paste the output to your SSH keys on the remote server (e.g. GitLab or GitHub).
Creating a New Repository
mkdir myrepo
cd myrepo
git init
# create or add files
echo hello > foo.txt
git add .
git commit -m "initial commit"
Push an Existing Repo to a Remote
git remote add origin remote-repo
git push --all –u origin
Downloading a Repository
git clone remote-repo
…where remote-repo is a path of the form user@server:/path/to/repo
Viewing Changes
git status- View list of changed files
git diff- View changes to files in the working directory
git diff --cached- View changes between index and HEAD commit
Committing Changes
git addfilegit commit- Add changes in file to the index, and then
commit staged changes in the index to the local repo git commitfile- Same as the above two commands, except file must already be tracked
To commit all changes to tracked files and new or removed files:
git add --all
git commit -m "commit message"
Commit all changes (to tracked files only):
git commit -a -m "commit message"
Branches
git branchbranch- Create a new branch named branch at the HEAD (current commit)
git checkoutbranch- Checkout (i.e. switch to) branch
git checkout -bbranch- Same as the above two commands run together, i.e. create a new branch named branch at the current commit and then check it out
git branch -dbranch- Delete the branch named branch
Merging Branches
To merge branch2 into branch1:
git checkout branch1
git merge branch2
Undoing Commits
git resetcommit- Rewind the current branch to commit, for example
git reset HEAD^rewinds the last commit. (never do this on published commits!) git revertcommit- This does not do what you would think it does. This creates a new commit to undo the changes of a previous commit.
Viewing History
git log- List commit history of the current branch
git log --oneline- Show one per line
git log --followfile- Show history of file
git showref- View changes in commit
git blamefile- See who changed what (and when) in a given file
git diffA…B- Compare two branches
Rebase
Doing a rebase sequentially regenerates a series of commits onto another branch.
git checkout B
git rebaseA- Rebase branch B onto A
git rebase --ontoA C [B]- Rebase branch B starting at commit C onto branch A. If B isn’t specified, rebase up to and including HEAD.
Pushing and Pulling
git push- Upload commits to default upstream remote repository (To set default upstream:
git push -uremote branch) git pushremote branch- Push new commits on branch to remote, e.g.
git push origin master git pull- Pull the latest commits from the upstream repository (does a fetch and merge)
git pullremote branch- Pull latest commits on branch from remote
Restoring Files
git checkoutcommit--file- Restore file from the given commit
git checkout HEAD --file- Discard uncommitted changes to file
git reset --hard HEAD- Discard all uncommitted changes
Staging Files
git addfile- Add changes in file to index
git resetfile- Unstage file (remove file from index), e.g. to keep it from being committed when you do
git commit
Resolving Merge Conflicts
git status- List the files with conflicts
vimfile- Edit files to fix conflicts…
<<<<<<< HEAD text changed in current branch ======= text changed in other-branch >>>>>>> refs/heads/other-branch
…or use a dedicated merge tool:
git mergetool
Then, git add file to mark each file resolved and finally git commit to conclude the merge. Alternatively, run git merge --abort to cancel the merge.