Adapted from Nice Reidman's interactive Git tutorial
"Learn git concepts, not commands"
-
Fork https://github.com/john-french/git_training
- creates your own remote copy of a repository
-
Get your copy of the remote repo onto your machine
git clone https://github.com/{YOUR USERNAME}/git_training.git- creates a new directory called
git_trainingon your machine
- Move to the repo directory and add a new file
cd git_training
echo "This is Bob" > Bob.txt- Check the status of your working directory and view changes
git status
git diff- Add changes to the staging area
git add Bob.txt
git diff --staged- Commit changes to the local repo
git commit -m "Added Bob"- Push changes to the remote
git push$ git log --oneline -5 --author pwebb --before "Sat Aug 30 2014"
5ba3db6 Fix failing CompositePropertySourceTests
84564a0 Rework @PropertySource early parsing logic
e142fd1 Add tests for ImportSelector meta-data
887815f Update docbook dependency and generate epub
ac8326d Polish mockito usage
- Create a new branch called
updateAlice
git branch updateAlice- Switch to the branch we just created
git checkout updateAlice- Switch back to master
git checkout master
git branch- Undo and use a shortcut instead
- Delete branch
updateAlice
git branch -d updateAlice- Create and update branch in one go
git checkout -b updateAlice
git branch- Make a change to
Alice.txtby adding more text
echo "More content added" >> Alice.txt- Add and commit this change
git add Alice.txt
git commit -m "Updated Alice"- Push these changes to the remote repository
git push
fatal: The current branch updateAlice has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin updateAlice- List remote branches
git branch -aFast-Forward Merge
- When no new commits have been added to the base branch
3 Way Merge
- When new commits have been added to the base branch
- See the
Updated Alicein commit history onupdateAlicebranch
git log- Checkout master, notice that commit isn't on the master branch
git checkout master
git log- Merge the
updateAlicebranch intomaster
git merge updateAlice
git log
- Create new branch
git branch newAlice- Make changes on master
echo "Some more stuff" >> Bob.txt
git add Bob.txt
git commit -m "Updated Bob"- Checkout
newAlicebranch and make changes there
git checkout newAlice
echo "Even more stuff" >> Alice.txt
git add Alice.txt
git commit -m "Updated Alice again"-
masterandupdateAlicehave now diverged -
Switch back to
masterand mergenewAlice
git checkout master
git merge newAlice- Notice new merge commit
Merge branch newAlice
git log --pretty=short
commit 5296df5d5e21747385eed68b1195b05dfce99cfc (HEAD -> master)
Merge: 0c34535 e849430
Author: John French <john.french@gmit.ie>
Merge branch 'newAlice'- Create and checkout a new branch and make some changes on it
git checkout -b fixBob
echo "Hi! I'm Bob. I've been here for a while now." > Bob.txt
git add Bob.txt
git commit -m "Bob's here for a while"- Checkout master and make changes to the same file
git checkout master
echo "Hi! I'm new here." > Bob.txt
git add Bob.txt
git commit -m "Bob's new here"- Merging
fixBobwon't work, files have conflicting changes
git merge fixBob
Auto-merging Bob.txt
CONFLICT (content): Merge conflict in Bob.txt
Automatic merge failed; fix conflicts and then commit the result.- View contents of
Bob.txt
cat Bob.txt
<<<<<<< HEAD
Hi! I'm new here.
=======
Hi! I'm Bob. I've been here for a while now.
>>>>>>> fixBob
- Open file in text editor and fix conflict manually (or using a tool)
- Add and commit fixed file
git add Bob.txt
git commit- Too messy! Cancel the merge
git merge --abortKeeping your copy of the repository up-to-date
- Save work in progress that's not ready to commit
git stash- List stashes
git stash list- View a stash
git stash show- Apply a stash (retrieve the changes)
git stash apply
- Git aliases are command Shortcuts
- use sparingly or you'll forget the real syntax
$ git config --global alias.co checkout
$ git config --global alias.ci commit
- Useful for longer commands
- Keep notes of new commands you learn
- Build up a library of handy commands
| Action | Git Syntax |
|---|---|
| List tags and commit ids | git show-ref --tags |
| Squash last 4 commits into one commit with new message | git reset --soft HEAD~4 |
| Amend last commit message | git commit —amend |
| Force push to remote (overwrites history, Don't do this!) | git push -f origin |
| List tags | git tag -l |
| Check out tag | git checkout tags/<TAG> |
| Show history in format [short SHA1] [message] | git log --oneline |
| Show sha1, author and message of a single commit | git log --pretty=short -n 1 <SHA1> |
| Check if object is present in repo and return its type | git cat-file -t [SHA1] |
| Get last common ancestor of two branches | git merge-base branch1 branch2 |
| Abort a merge | git merge --abort |
| Create a new branch and check it out | git checkout -b branch_name |
| List remotes | git remote -v |
| Show details of remote | git remote show remote_name |
| Delete branch from remote | git push origin :branch_name |
| Resolve all conflicting files below a directory with the remote side of the merge (use ours for the local side) | `grep -lr '<<<<' . |
| Resolve conflics in specific file/directory | git mergetool <file/directory> |
| Reset resolved merge conflict to conflicted state | git checkout -m <file> |
| Remove local branches no longer present on remote | git remote prune origin |
| Reset local repo to nth commit behind current HEAD | git reset --hard HEAD~n |
| Revert commit without committing | git revert -n <sha1> |
| Stage changes interactively | git add -p |
| Checkout previously checked-out branch | git checkout - |
| Search in git command history | CTRL-R |
| Check out file from another branch | git checkout <branch_name> -- <paths> |
| Cherry pick a merge commit, appending a line saying “cherry picked from commit” | git cherry-pick -x -m 1 <sha1> |
| Merge a file from another branch | git checkout solution; git checkout master README.md; git add README.md; commit -m "Merged README from master" |
| Create and apply a binary patch | git diff-index 79fd4d7 --binary > ~/Desktop/my-patch, Where 79fd4d7 is a placeholder for the commit that came right before the range of commits you want to diff. |
















