Learn Git
Table of Contents
What is git #
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Start #
There are two main ways to start with a git repo.
Option 1
Getting started you can use the command git init
to create a new repository. Which will be a folder named .git
.
Option 2
Or you can cdownload a repo from github for example git clone /path/to/repository
or git clone username@host:/path/to/repository
.
Workflow #
Three “trees” maintained by git
- Working Directory = Holds the actual files
- Index = which acts as a staging area
- HEAD = points to the last commit you’ve made
Add & commit #
You can add either individual files with git add <filename>
or all files + directories with git add .
. This adds the files to the Index or staging area.
To commit to the HEAD use git commit -m "Commit message"
.
If not a cloned repo add a remote repository git remote add origin <server>
.
Then to add changes to the remote repo use git push origin master
.
branching #
branches are used to develop features isolated from each other. The main branch is the default branch when you create a repository. Use the other branches for dev then merge back to main.
- Create a new branch
git checkout -b <branch>
- Switch branches
git checkout <branch>
- Delete a branch
git branch -d <branch>
- Push to repo
git push origin <branch>
Update & Merge #
- update local repo with newest commit
git pull
- merge branch into your active branch
git merge <branch>
- preview
git diff <source_branch> <target_branch>
Rebase #
Rebase is helpful to edit commits
first run git reflog
. Find the the commit farthest from the current that you want to include. Then type git rebase -i HEAD~{farthest}
.
So if you wanted to rebase the past 3 commits run git rebase -i HEAD~3
.
It will give you something like the below
pick f7f3f6d Change my name a bit
pick 310154e Update README formatting and add blame
pick a5f4a0d Add cat-file
# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Note that the commits are in reverse order, the top is the least recent and the bottom is most recent.
To merge into one commit change the 2 most recent actions from pick
to squash
.
pick f7f3f6d Change my name a bit
squash 310154e Update README formatting and add blame
squash a5f4a0d Add cat-file
Then save!
More info