What is Git?
Git is a distributed version control system. It is free and open source, created by Linus Torvalds.
Remember Github is just one various remote options. It is not
git
– it uses git to store git repos and adds some special sauce on top. It is in the same class as: gitlab, gogs, gitea and even a git instance accessible over ssh.
It is different from previous version control techniques:
- Different users maintain their own repositories.
- There is no single failure point.
- Changes are stored as change set.
- Change sets can be exchanged between repositories.
- Branches can be used, with fast context switching, to control your working development directory.
- Much faster than any other version control system.
- No network access required.
Git is for anyone who wants to track changes, it was designed by developers for developers but anyone wanting to track changes can use it. However Git is no good for:
- Images
- Movies
- Fonts
- Music
- Microsoft Office (Word, Excel, etc)
Git Quick Reference
Download Git
go to: http://git-scm.com
download and install with Git Bash
Now open Git Bash and go to the folder where you want a new repository:
Initialise a project Git:
git init
Staging Changes Git:
git add .
Commiting Changes Git:
git commit -m "Message"
How to write commit messages Git:
- Substitute “Message” above with:
- A short single line description of your changes or actions
- Optionally followed by a blank line and a more complete description
- Each line should be less than 72 characters
- Present Tense
- Asterix, Hyphen or ‘>’ as bullets
- It is not email, no dates or times
View change log Git:
git log
Change log additional commands Git:
#single line per commit
git log --oneline
#show just 3
git log --oneline -3
#since
git log --since="2012-02-20"
#until
git log --until="2 weeks ago"
#author
git log --author="Name"
#Searching
git log --grep="test"
#difference of log
git log -p
#statistics
git log --stat --summary
#email format
git log --format="email"
#graph
git log --graph
#well formatted (works better on unix)
git log --oneline --graph --all --decorate
Architectural differences between Git and other Version Control Git:
Other version control:
- Two-tree Architecture
- Repository -> checkout -> sandbox -> commit -> repository
Git:
- Three-tree Architecture
- Repository -> checkout -> sandbox -> staging index -> commit -> repository
Git changes and Checksums Git:
- When anything is changes in Git, a change set is created.
- Git converts the change set into a SHA-1 checksum
- Every change including the change message has a unique checksum
- Therefore is maintains the integrity of changes
Head in Git:
head is a pointer to the tip of the current branch in the repository
Git Status:
Shows current branch and all untracked files (changes not yet staged)
git status
Differences in Git:
#working directory
git diff
#staged differences
git diff --
#differences between branches
git diff master..sandbox
Deleting in Git:
#Unstaged
#just delete the file
#staged differences
git rm <filename>
git commit -m "Message Delete"
Renaming in Git:
git add <renamed>
git rm <old file>
git commit -m "Rename Message"
Moving Files in Git:
git mv <onefile.txt> <otherfile.txt>
Converting a Real Project to a repository (Skipping Staging) iwth Git:
git init
#skip add just commit
git commit -a
Undo Unstaged Changes with Git:
git checkout -- <Filename>
Undo Staged Changes with Git:
git reset HEAD <Filename>
Undo Commits with Git:
- Changing the latest commit forces all other commits down the line to change due to checksums
- You can change the last commit (at HEAD)
- Below can be used just to change the last commits message
git commit --amend -m "Amend Message"
Undo Commits of Specific Commit with Git:
git checkout <CommitSHA/Checksum> -- <Filename> #eg. git checkout 4d5shd6a -- test.php #or git checkout b47hsdf4 #Use: git log to get the SHA/Checksum
Revert to Specific Commit with Git:
#stage and commit git revert <Commit SHA / Checksum> #no commit no stage git revert -n <Commit SHA / Checksum>
Reset in Git:
#USE WITH CAUTION - Changes position of HEAD git reset --soft git reset --mixed (default) git reset --hard
- Soft: Moves Head Pointer, No staging, No Changes in Working Directory.
- Mixed: Moves head pointer, changes staging directory to match repository, No Changes in Working Directory.
- Hard: Changes Staging and Working Directory.
Remove Untracked Files from Working Directory in Git:
#test run git clean -n #force to run git clean -f
Ignoring Files in Git:
#in project/ .gitignore #if not there create #basic regs: * ? [aeiou] [0-9] *.php
Typical things to ignore with .Gitignore:
- Compiled Source Code
- packages
- Compressed Files
- Logs
- Databases
- Operating System Generated Files
- user-Uploaded Assets (Images, PDF, Videos)
Global Ignores in Git:
#personal preferences specific to OS git config --global core.excludesfile /users/Username/.gitignore_global
Remove just from Staging index in Git:
git rm --cached <Filename>
Tracking an Empty Directory Git:
#Place a small file in the directory, Git only tracks files not directories
Treeish – what is it with Git:
Treeish: References part of a tree
Can Use:
- full SHA-1 Hash
- short SHA-1 Hash (4 to 15 characters)
- HEAD pointer
- branch reference
- parent commit: -HEAD^ or -HEAD~2
- tree is a directory, blob is a file
git ls-tree <treeish> git ls-tree HEAD
Branching in Git:
- Don’t use much resources
- Easy to Create
- Fast
- Little space required
- Ability to try new ideas and test
- Can create for specific parts of project
- One working Directory
- Fast context switching
#check branches git branch #new branch git branch sandbox #switching between branches git checkout master #create and switch branch git checkout -b master
Checkout Files in Git:
git checkout -- <Filename>
Finding if branch which has all commits within:
git branch --merged
Delete branch in Git:
git branch -d <branchname>
Merging branches with Git:
git merge <branchname> #fast forwardn is not a true merge #no fast forward git merge --no-ff <branchname> #fast forward only git merge --ff-only <branchname>
Merge Conflicts in Gits:
- Go through and decide which changes you want
- abort merge
- Resolve Conflicts manually
- use merge Tool
Avoiding Merge Conflicts with Git:
- Keep lines short
- No unnecessary whitespace edits
- Keep focused do not wander around the project
- Merge Often
Stashing uncommited Changes in Git:
git stash save "Stash Message" git stash list git stash show -p stash{0}
Stash brought into any Repository:
#default puts latest one in git stash pop #put specific git stash pop stash@{2} #pops but keeps in stash git stash apply
Delete Items from Stash in Git:
git stash list git stash drop stash@{1} #clear stash git stash clear
Remote Servers:
- Used as convention Remote not necessarily required
- Git makes an origin/master referencing remote server
List of remotes:
git remote
Telling Git where the remote repository is, usually Github:
git remote add <name usually origin> <https address or ssl>
Pushing an Existing Repository or github:
#push masterbranch to remote git push -u origin master
Getting a repo from a remote:
#find project on git #go to folder you want it to reside git clone <https/ssl of project> <project name>
Pushing changes to Remote or Github:
git push origin master #if tracking branch git push
Fetching from remote repository or Github:
git fetch origin #fetch and merge git pull origin
Fetching Tips for Github and Remote Servers:
- Fetch before you work
- Fetch before you push
- Fetch often
Delete a remote branch from remote or Github:
#push nothing to origin git push :<branch> #new way git push origin --delete <branchname>
Using an IDE:
Many IDE’s have this functionality built in to make it much easier
More Info Git Github:
Git Official Tutorial