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 --systemstored in /etc/gitconfig - User configuration using
git config --globalstored in ~/.gitconfig - Repo configuration using
git configstored in .git/config in each repo
Commands
git config --listto see all configsgit config --global --listto see global configurationsgit config --list --show-originlist configs with location of file where specifiedgit config --global user.name "Claire Furney"to configure the user namegit config --global core.editor vimgit config --global help.autocorrect 1to configure fuzzy matching on git commandsgit config --global color.ui autogit config --global core.autocrlf inputcan be one oftrue|false|input(generally set totrueon Win andinputon 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..HEADto 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.txtwill by default checkout the HEAD version of a file and overwrite any local changesgit reset --headwill checkout all changes from HEAD, overwriting any local changesgit reset --soft HEAD~1will checkout the HEAD~1 commit with the latest changes previously committed stagedgit reset --hard HEADwill checkout the HEAD~1 commit and lose all previously committed changesgit cleancleans up a working copy and removes any unstaged filesgit clean -nshows what if informationgit clean -fforces the clean
Working Remotely with Git
git log --onelineshows commit messages on one linegit log --oneline | wc -lpipes this intowcto count the number of commit messages (-l= number of lines)git log --oneline --graphto show a simple graph of branchesgit log --oneline --all --decorateto show any tags, labelsgit log origin/masterto show the logsgit shortlogshows commits by authorgit shortlog -sneshows summary (no commit msgs), numerically by number of commits, emails includedgit show HEADto show details of the last commit, use HEAD~1 or HEAD~10 or just the sha hash to show other commitsgit remoteto show your remotesgit remote -vto show the details of the remotesgit reflogto 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 branchshows the local branchesgit branch -rshows the remote branchesgit branch -vgit 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 fetchgit merge origin/mastermerge 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 pullwill only work if you have already specified an upstream remote tracking branchgit pull origin masterif you haven’t specified the upstream branch - same as git fetch && git merge origin/mastergit remote rm originto remove a configured remotegit branch feature1to create a new feature1 branchgit checkout feature1to switch to itgit checkout -b feature2to create a new branch and switch to itgit branch fix1 a15ec6bto create a branch of a particular commit, and thengit checkout fix1git branch -m fix1 bug1234to rename a branch
Tags
Tags are labels on the sha1 commits which never change (unlike branches).
git tag v1.0git tag -a v1.0to add a messagegit tag -s v1.0to 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 stashto rollback any uncommitted changes on the current branch and stash those changesgit stash listto list current stashesgit stash applyto reapply the uncommitted changes from the stash, stash stays in stash listgit stash popto reapply the uncommitted changes from the stash, stash is removed from stash listgit stash dropto drop things from the stash listgit stash branch feature3to create a branch from a stash, stash is popped
Merging
git merge feature1will merge the feature1 branch in the the current local branchgit mergetoolto resolve any merge conflictsgit diff --cachedcompare the repo to the staging area
Rebasing
git rebase masterrebase 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 mergetoolto resolve thesegit rebase --continueto retry the rebase