Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active December 13, 2015 19:09
Show Gist options
  • Save elgalu/4960870 to your computer and use it in GitHub Desktop.
Save elgalu/4960870 to your computer and use it in GitHub Desktop.
My quiz on: Pro Git by Scott Chacon
##
- How is the git `staging area` a.k.a?
=>
# Is also known as the index
##
- List all available git configurations within current dir
=>
git config --list
#=>
merge.tool=meld
diff.external=/home/leo/gitvdiff
##
- Write .gitignore file lines to:
# Ignore *.a files but do track lib.o even though you're irgnoring .a files
=>
# You can negate a pattern with !
*.a
!lib.a
##
- Git has some file states, name diff bt commited vs modified vs staged
How are the working directory, the staging area and the git repo related to those?
=>
commited = data is safely stored in your local git db
modified = you have changed the file but have not commited yet
staged = marked the modified file to go into your next commit
# Directory relations:
## When you git checkout, you take from the git repo to your working dir
## When you stage files (git add), you move modified files from your working dir to the staging area
## When you git commit, you store staged data to the git repo (local)
##
- Write .gitignore file lines to:
# Only ignore the root TODO file, not subdir/TODO
=>
/TODO
##
- How can you show a result that tells you the changes bt what you have staged vs. what will go into your next commit
=>
# staged vs. commited
git diff --staged
# If git < 1.6.1 use the old command:
git diff --cached
##
- Write .gitignore file lines to:
# Ignore all files in the build/ directory
=>
build/
##
- Git. Keep `thefile` in your working tree but remove it from your staging area.
i.e. Keep thefile in your hard drive but do not have git to track it anymore
=>
git rm --cached thefile
##
- Git diff bt `git add .` vs `git add -A`
=>
# Same thing
##
- How can you show a result that tells you the changes you've made but you haven't yet staged?
i.e. your working dir vs. your staging area
=>
# working dir vs. staging area
# remember staging area == commited when nothing new is staged
git diff
##
- From git point of view, whare are files really?
=>
blob's
# i.e. files contents are stored as blob's in git:
Rakefile -> blob: a235b3
README -> blob: a325c1
##
- Git. Show the last 5 commits
=>
git log -n 5
##
- What are git trees?
=>
# A tree is git type of object that stores a simple list of trees and blobs
# Also stores some properties like file modes, sha's, tree name (dir name), blob name (filename)
# Trees are pointers to blobs or other trees
##
- Say you git commited with the wrong commit message
How can you change it?
=>
git commit --amend -m "New commit message"
##
- Say you git commited too early and forgot to add somefile
How can you fix that commit?
=>
# Make the changes you want and stage those changes.
# Then run
git commit --amend
##
- What does the following command?
git commit -a -m 'tap'
=>
# -a performs a `git add -u .` which stages every file that is already tracked
# -m provides a commit message 'tap'
# then commit
# You can faster write -a -m
git commit -am 'tap'
##
- Git show the last time each line of a file was changed and by whom
Is it possible to filter file lines bt 12 and 22?
=>
# Yes:
git blame -L 12,22 simplegit.rb
##
- Git show the commits and diffs made on some particular file
Can this be done with gitk?
=>
git log -p thefile
# More nicely with gitk
gitk thefile.name
##
- Git. Show all commits within the last 2 weeks
=>
git log --since=2.weeks
##
- What does the following command?
git rm thefile
=>
# remove thefile from staging area
# remove thefile from working dir
##
- Add a new remote Git repository as a shortname you can reference easily
=>
git remote add [shortname] [url]
##
- Rename somefile in git
=>
git mv somefile newname_or_dir
##
- Fetch any new work that has been pushed to remote server `origin`
since you cloned it, or last fetched from it.
=>
git fetch origin
##
- What does the following command?
git init
=>
# Initializes a new git repository creating:
.git/
##
- Explain the following scenario:
git commit -m 'initial'
git add somefile
git commit --amend
=>
# All three commands end up with a single commit
# `somefile` is on the 'initial' commit even though it was added later
##
- Automatically fetch and then merge a remote branch into your current branch
=>
git pull
##
- Show the general form of `git push` command
=>
git push [remote-name] [branch-name]
##
- What does the following?
git remote show origin
=>
# Shows more syncing info on one particular remote (origin in this case)
- which multiple branches are automatically merged when you run git pull
- which branch is automatically pushed when you run git push on certain branches
- which remote branches on the server you don’t yet have
- which remote branches you have that have been removed from the server
##
- Given the following scenario, make j.rb to be on a separate commit.
git add .
git status
# Changes to be committed:
# modified: i.rb
# modified: j.rb
=>
git reset HEAD j.rb
git status
# Changes to be committed:
# modified: README.txt
#
# Changed but not updated:
# modified: benchmarks.rb
##
- Is there a way to rename `origin` remote reference to something else like github for ex?
=>
git remote rename origin github
##
- Rename a git branch named `elgalu` into `oct2-orange`
=>
git branch -m elgalu oct2-orange
##
- Discard the changes you made to somefile
=>
# Discard changes in working directory and lose them forever
git checkout -- somefile
##
- Git list which remote servers you have configured
=>
git remote -v
origin git://github.com/some-repo.git
# origin is the default name Git gives to the server you cloned from
##
- List the available Git tags on current project
=>
git tag
##
- Git remove a remote reference for some server that is for ex. no longer available
=>
git remote rm oldserver
##
- List all git tags that match /^v1.4./
=>
git tag -l 'v1.4.*'
##
- What is the Git diff bt lightweight and annotated tags?
How you create each one?
=>
# a lightweight it’s just a pointer to a specific commit
git tag v1.4
# an annotated are stored as full objects in the Git db
# tagger name, email, date
# tagging message
git tag -a v1.4 -m ’my version 1.4’
##
- Create a git alias to make possible:
git unstage [filename]
=>
git config --global alias.unstage 'reset HEAD --'
##
- Is it correct to say that a branch in Git is simply a lightweight movable pointer to one of your commits?
How you create a new branch?
=>
# yes, and every time you commit, that pointer moves forward automatically
# This will create the branch but won't change to it
git branch testing
# If you want to create the branch an immediately change to it:
# i.e. update HEAD to point to: refs/heads/test
git checkout -b testing
##
- On common git workflows, explain for what would you use these branches:
master, develop, [short-lived-ones]
=>
master
- code that is entirely stable
- code that has been or will be released
develop
- code that will be merged into master as soon as is stable
[short-lived-ones]
- branches you create and use for a single particular feature or related work
##
- Is it correct to say that remote branches are local branches that you can’t move?
That they’re moved automatically whenever you do any network communication?
=>
# Yes
##
- Get a Git list of merges / list of merge commits
=>
git log --merges
##
- Given the following scenario:
git lola
* 42b2 (featureA) will use to cherry pick
* 11a1 (HEAD, master) initial commit
# Show the project history after
git checkout master
git cherry-pick 42b2
git lola #=> ?
# with: git config --global alias.lola "log --graph --decorate --pretty=oneline --abbrev-commit --all"
=>
# Note that even 42b2 commit == 85c7, they end up as diff commits
* 85c7 (HEAD, master) will use to cherry pick
| * 42b2 (featureA) will use to cherry pick
|/
* 11a1 initial commit
##
- What does the following?
git log --not 9305e
=>
# Show new stuff since commit 9305e
# You can use the sha1 or tag or branch name
##
- What is git reflog?
=>
# Is a reference log of where your HEAD and branch refs have been for the last few months
# This info is strictly local (not remote)
##
- Use git reflog to see where your master branch was yesterday
=>
git reflog master@{yesterday}
##
- Git. Show the parent commit of 831b
=>
git show 831b^
##
- Git. See all commits in develop that aren’t in master
=>
git log master..develop
# or:
git log develop --not master
##
- Git. See all commits in master that aren’t in develop
=>
git log develop..master
# or:
git log master --not develop
##
- Git. See what you’re about to push to a remote (master branch)
=>
git log origin/master..HEAD
# or:
git log HEAD --not origin/master
##
- Git. see all commits that are reachable from refA or refB but not from refC
=>
git log refA refB --not refC
# or:
git log refA refB ˆrefC
##
- Git. See what is in master or develop but not any common references
=>
git log master...develop
##
- Git. Stash your uncommited local changes before changing branch
=>
git stash
##
- Git. Bring back your stashed changes, if any
=>
git stash apply
##
- Git. See which stashes you have stored
=>
git stash list
##
- Git. Delete all saved stashes
=>
git stash clear
##
- Git. Delete a particular stash, in this case the last one
=>
git stash drop stash@{0}
##
- Git. Save a named stash
=>
git stash save "some personal identifier"
##
- Git. Apply one particular stash, e.g. stash 2
=>
git stash apply stash@{2}
##
- Git. Apply the last stash and remove it (one linear)
=>
git stash pop
##
- Git. Appy stash but to a new branch named testchanges
=>
git stash branch testchanges
##
- How does Git know what branch you’re currently on?
=>
# It keeps a special pointer called HEAD
# HEAD is a pointer to the local branch you’re currently on
##
- Git delete a tag 'v0.0.1' local and remotely to `origin`
=>
git tag -d v0.0.1
# To delete the tag remotely:
git push origin :refs/tags/v0.0.1
##
- When you create a new brach with `git branch [branch-name]`
Does it switches HEAD to that new branch? how?
=>
# Nope, it only creates the branch but keeps HEAD where it was
# To switch to the newly created branch:
git checkout [branch-name]
##
- What happens when you run `git branch` with no arguments?
=>
# simply lists your current branches
##
- Given a remote named `origin`. Explain this git expanded ref:
refs/heads/serverfix:refs/heads/serverfix
=>
# Take my serverfix local branch and push it to update the remote’s serverfix branch
# These is achieved with either:
git push origin serverfix:serverfix
# Or simply
git push origin serverfix
##
- How can you git 'join' the last two commits into 1?
=>
git rebase -i HEAD~2
##
- Show full diff of what would happen if you were to merge this topic branch into master
The work you’ll introduce if you merge this branch with master.
git checkout contrib
=>
git diff master...contrib
##
- Git. See the tag data along with the commit that was tagged for tag 'v1.4'
=>
git show v1.4
##
- Create a git branch and switch to it at the same time
=>
git checkout -b issue55
# Which is just a shortcut for:
git branch issue55
git checkout issue55
##
- List all git branches including last commit of each branch
=>
git branch -v
##
- Given
git fetch origin
# * [new branch] serverfix -> origin/serverfix
What local branches are created?
=>
# None, just the branch origin/serverfix is created
# If you want to track remote branch refs/remotes/origin/serverfix
git checkout -b serverfix origin/serverfix
# Or simply
git checkout --track origin/serverfix
##
- What is a Git bare repository?
=>
# Is a Git repo that has no working dir
# i.e. The contents of you .git/ dir, nothing else
# Generally, only remote repos are bare
##
- Tag a commit `9fce` as 'v2.0' even though is an older commit
=>
git tag -a v2.0 9fce
##
- Write a Git alias to see the last commit easily with `git last`
=>
git config --global alias.last 'log -1 HEAD'
##
- Given you are working of a git branch named `hotfix`
Switch back to master branch and merge those hotfix changes to deploy
Then delete that hotfix branch
=>
git checkout master
git merge hotfix
git branch -d hotfix
##
- List all branches that contain work you have not yet merged into your current git branch
=>
git branch --no-merged
##
- What is a git a tracking branch?
=>
# Are local branches that have a direct relationship to a remote branch
# The make possible to write these commands without much further details
git pull
git push
##
- How can you git delete a remte branch? Explain the ofuscated syntax for doing so
=>
# To delete remote branch serverfix on origin:
git push origin :serverfix
# General push syntax is:
git push [remotename] [localbranch]:[remotebranch]
# So if no [localbranch] is given then you are sayng make nothing to be remotebranch
##
- What is the big do-not-do regarding git rebasing?
=>
# Do not rebase commits that you have pushed to a public repository
# Cause when rebasing you’re abandoning existing commits and creating new diff ones
##
- How can you check if you’re about to git commit whitespace issues that may annoy other developers
=>
# --check identifies possible whitespace errors and lists them for you
git diff --check
# hola.txt:2: new blank line at EOF.
##
- Does `git push` transfer tags to remote servers by default?
=>
# No, you'll have to explicitly push tags with:
git push [remote_name] [tag_name]
# e.g.
git push origin v2.0
# Or simply push all tags with:
git push origin --tags
##
- How can you use a graphical tool to resolve git merging conflicts?
=>
git mergetool
##
- Explain the following error and how to avoid it:
git branch -d testing
# error: The branch ’testing’ is not an ancestor of your current HEAD.
=>
# Fails because it contains work that isn’t merged in yet
# If you are sure you want to delete it, run
git branch -D testing
##
- What does the following?
git push --force
=>
# It forces to overwrite the commits history on the server
##
- What is the diff bt
git log origin/master
git log remotes/origin/master
git log refs/remotes/origin/master
=>
# They're equivalent because git expands each of them to:
refs/remotes/origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment