Pluralsight: Git Fundamentals
History
- 1st Generation - single file
- 2nd Generation - multiple file - centralized e.g. SVN, TFS
- 3rd Generation - works on change sets - distributed e.g. Git, Hg, Bazaar, BitKeeper
Configuring Git
Config files
- System configuration using
git config --system
stored in /etc/gitconfig - User configuration using
git config --global
stored in ~/.gitconfig - Repo configuration using
git config
stored in .git/config in each repo
Commands
git config --list
to see all configsgit config --global --list
to see global configurationsgit config --list --show-origin
list configs with location of file where specifiedgit config --global user.name "Claire Furney"
to configure the user namegit config --global core.editor vim
git config --global help.autocorrect 1
to configure fuzzy matching on git commandsgit config --global color.ui auto
git config --global core.autocrlf input
can be one oftrue|false|input
(generally set totrue
on Win andinput
on Mac OSX)git config --global alias.lga "log --graph --oneline --all --decorate"
More specific override more general, so you can override certain settings just for a particular repo.
Working Locally with Git
git init
- initialise a new repogit add README.txt
- add a new file to be staged for commitgit add -u
- stage any updated files (will notice updates or deletes, any files it already knows about)git add -A
- add all files including any untracked ones (careful not to add anything you don't want tracked)git log
- to show commit historygit diff dd6819a..a15ec6b
- to diff two commitsgit diff HEAD~1..HEAD
to diff the penultimate commit (HEAD~1) and the latest commit (HEAD)
By staging and commiting in two parts, you can stage and commit different unrelated changes separately.
Use rm
or mv
as normal to delete or rename files, stage any changes and commit them, Git will recognise renames as such.
git checkout README.txt
will by default checkout the HEAD version of a file and overwrite any local changesgit reset --head
will checkout all changes from HEAD, overwriting any local changesgit reset --soft HEAD~1
will checkout the HEAD~1 commit with the latest changes previously committed stagedgit reset --hard HEAD
will checkout the HEAD~1 commit and lose all previously committed changesgit clean
cleans up a working copy and removes any unstaged filesgit clean -n
shows what if informationgit clean -f
forces the clean
Working Remotely with Git
git log --oneline
shows commit messages on one linegit log --oneline | wc -l
pipes this intowc
to count the number of commit messages (-l
= number of lines)git log --oneline --graph
to show a simple graph of branchesgit log --oneline --all --decorate
to show any tags, labelsgit log origin/master
to show the logsgit shortlog
shows commits by authorgit shortlog -sne
shows summary (no commit msgs), numerically by number of commits, emails includedgit show HEAD
to show details of the last commit, use HEAD~1 or HEAD~10 or just the sha hash to show other commitsgit remote
to show your remotesgit remote -v
to show the details of the remotesgit reflog
to show the log of where HEAD has been pointing
Git Protocols
- http(s) on ports 80 and 443 e.g. https://github.com/jquery/jquery.git - read/write; password for auth
- git protocol on port 9418 e.g. git://github.com/jquery/jquery.git - read only; anonymous only
- ssh on port 22 e.g. git@github.com:jquery/jquery.git - read/write; ssh keys for auth file e.g. /Users/claire/repos/jquery - read/write; local only
Branches
Branches are simply labels on the sha1 commits, but following along with more recent commits.
git branch
shows the local branchesgit branch -r
shows the remote branchesgit branch -v
git remote add [remote-alias] [remote-url]
if you use the ssh version it will be able to push without creds if ssh keys have been set upgit fetch
git merge origin/master
merge from origin/master in to current local branch - get a message fast-forward, then no merging / commiting needed to be done and git was able to just move the HEAD pointer to the latest commitgit pull
will only work if you have already specified an upstream remote tracking branchgit pull origin master
if you haven't specified the upstream branch - same as git fetch && git merge origin/mastergit remote rm origin
to remove a configured remotegit branch feature1
to create a new feature1 branchgit checkout feature1
to switch to itgit checkout -b feature2
to create a new branch and switch to itgit branch fix1 a15ec6b
to create a branch of a particular commit, and thengit checkout fix1
git branch -m fix1 bug1234
to rename a branch
Tags
Tags are labels on the sha1 commits which never change (unlike branches).
git tag v1.0
git tag -a v1.0
to add a messagegit tag -s v1.0
to sign a tag
Stash
Useful way as a temporary holding area for changes you don't want to commit to a branch, but you don't want to lose.
git stash
to rollback any uncommitted changes on the current branch and stash those changesgit stash list
to list current stashesgit stash apply
to reapply the uncommitted changes from the stash, stash stays in stash listgit stash pop
to reapply the uncommitted changes from the stash, stash is removed from stash listgit stash drop
to drop things from the stash listgit stash branch feature3
to create a branch from a stash, stash is popped
Merging
git merge feature1
will merge the feature1 branch in the the current local branchgit mergetool
to resolve any merge conflictsgit diff --cached
compare the repo to the staging area
Rebasing
git rebase master
rebase your changes on top of master - useful after pulling in any recent repo changes to master. If there are any conflicts with this, then rungit mergetool
to resolve thesegit rebase --continue
to retry the rebase