Do you know that questions about git get the most views on StackOverflow? Iβve searched a lot on Google how to execute certain actions with git, and this actually slowed me down a lot. There are some actions that we tend to use a lot, so itβs good to learn them. Here are my favorites, learning from friends and internet, hope you find them useful.
Before we begin, you should run git βversion to check your current git version, mine is 2.12.2 as in macOS High Sierra. Here is the official git documentation, you can read details about git commands, parameters and new releases of git.
Create the branch on your local machine and switch in this branch:
git checkout -b branch_name
Create branch from commit:
git branch branch_name sha1_of_commit
Push the branch to remote:
git push origin branch_name
Rename other branch:
git branch -m old new
Rename current branch:
git branch -m new
Rename remote branch:
git branch -m old new # Rename branch locally
git push origin :old # Delete the old branch
git push --set-upstream origin new # Push the new branch, set local branch to track the new remote
Doing things in command line is cool and faster. However for viewing branches and commits, I find using a GUI client more visualizing and comfortable. You can see a list of all GUI clients here, I myself use SourceTree.
Check before you commit
We usually have some experiment code that we donβt want they to step into our commit. I usually mark my experiment with // but sometimes forget to unstage that.
Starting with 2.9, Git has improvement on its commit hook which makes it globally using hooksPath.
Firstly we nee to create a file called pre-commit, and place it into, for example, /Users/khoa/hooks:
if git rev-parse --verify HEAD >/dev/null 2>&1 then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi
# The special marker tag to mark things which we still need to change marker="<TEST>"
# Redirect output to stderr. exec 1>&2
iftest $(git diff --cached -z $against | grep $marker | wc -c) != 0 then cat <<\EOF Error: Still has invalid debug markers in code: EOF echo `git diff --cached -z $against -G $marker` exit 1 fi
In your project, run git config core.hooksPath /Users/khoa/hooks.