Learning by doing.
First, we have the basic workflow as below
git init git add . git commit -m "some message" git remote set-url origin https://github/com/USERNAME/REPOSITORY.git git push origin master
Sometimes, after a commit, if we want to modify the commit message, we can
git commit --amend
Sometimes, we messed up with the current modification and want to throw away all the changes, we can use
reset --hard HEAD
to throw the unstagged changes.
In some cases, we want to combine several commits into one to keep the repo clean, we can use
For example, after
git rebase -i HEAD~2
we will enter into the commit step automatically, where we can see two latest commits, and we change the status of the latest commit into
squash so as to combine it with the previous commit.
After that, we can modify the commit message, and it will be ready to push to GitHub.
Sometimes, we may need to overwrite the commits on Github after
rebase, then just
git push origin master -f
to force the push.
Branch and merge
It is better to keep the
master branch ready to work all the time, so when we make our modifications, we’d create a branch by
git checkout -b new_feature
After a new commit from this new branch, and we think the new feature is ready to be released, we can merge back to
git checkout master git merge new_feature
If there are some conflicts, deal with ti, and the
master will be merged with
new_feature, and we can delete the
new_feature branch by
git branch -d new_feature
.gitconfig file is used for configurations.
For example, we can use
alias to simplify our commands.
Edit the file
~/.gitconfig, add the lines below
[alias] ci = commit -a -v throw = reset --hard HEAD
and we can use
git ci to commit and enter the message editting step, and
git throw to reset the modifications.
There are many other options we can use in
More details please refer to this doc.
Collaborate with others
Fork and pull request
First, fork others’ repo on GitHub, then clone the forked repo to local and make our contribution.
When it is ready, push our updates to the forked repo according to the steps above.
Then we need to pull request from the forked repo on GitHub, after that, we will deal with conflicts and merge.
Keep the fork up to date
After the merge, we can delete the forked repo, or, if we still want to make contribution to the original repo, we can keep updated if the original repo gets updated after our push. In other words, if our forked repo is several commits behind the original repo, we can keep the fork up to date by
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git git fetch upstream git pull
After that, our forked repo will be updated and you can continue our contribution to the project!