Git tips
19 Jun 2015Tips for myself for git

git add
-
git add -N <file>adds a file, but does not stage the changes in the file to the index. This allows you to split the changes to this file into atomic commits -
git add -Aadds and removed all added and deleted files from the index and stages all changes. It is not equivalent tohg addremovebecause it stages all changes to the index
git commit
-
git commit -astages all changes to tracked files and removes any deleted files from the index. This is equivalent togit add -u; git commit -
git commit --amendappends the current staged edits to the previous commit, creating a new commit. Add--no-editto keep the same commit message
git checkout
git checkout <rev> -- <file>, leave<rev>empty if you want to update to the current index, or useHEADto update to the local repository
git diff
diff --git a/importantFile b/importantFile
new file mode 100644
index 0000000..3d44acd
--- /dev/null
+++ b/importantFile
@@ -0,0 +1,2 @@
+important stuff
+crazy change- Notice the
0000000..3d44acdis not the commit hash, but the file hash
git log
-
git log --graph --decorate --oneline $(git rev-list -g --all)shows all the branches, including unnamed ones and stashes -
git log -pshows detailed commits
git push
-
git push --all originpushes a new branch (don’t necessarily need origin - only if you have multiple upstreams) -
git push --set-upstream origin thirdbranchpushes the current branch and sets the remote as the upstream remote repository -
git push origin masterwill push themasterbranch toorigineven if you are in a detachedHEADstate
git rebase
The standard workflow is as follows - we start with HEAD on the master branch tip. Then:
git checkout -b feature master # create a branch at the same point as the master branch
# add feature
git commit -am "Started developing a new feature"
# go back to master to make an important change - do this in another branch
git checkout -b quickfix master
# fix critical bug
git commit -am "Fixed bug"
git checkout master
git merge quickfix
git branch -d quickfixNow, we have a fork. We could do a straight merge of the feature, or we could use a rebase - this retains linear history. It must be certain that no one else has pulled the feature branch, as this will be removed and re-written by a set of entirely different commits
git checkout feature
git rebase masterAt this point, the history looks like
* cc2bfa3 (HEAD, feature) Started developing a new feature
* b000ff1 (master) Fixed bugNow, we have to do a fast forward merge (this is an easy merge, it’s just moving the branch head location forward a few commits)
git checkout master
git merge feature # Deal with potential merge conflicts in the standard way
git branch -d featureAnd we have fully integrated the feature into the main branch.
git rebase -i
Still need to cover this
git reset
git resetunstages all files, allowing you to recreate the commit snapshot from scratch, but it leaves the working directory unchanged. Adding--harddestroys all changes in the working directory, so there are no differences to be unstaged. It resets the working directory to the original commit