May 1, 2019

266 words 2 mins read

Git cheatsheet

Some git commands and tricks

when forking set locally el origin as upstream:

master => git remote add upstream https://github.com/kubernetes/website.git git pull upstream master

website git:(branch-to-update) git pull –rebase origin master

  • to display last 10 commits:

    git log --pretty="%h - %s" -10
    
  • to squash the last two commits

    git rebase -i HEAD~2 
    

it will open a vi editor, follow the instructions and change the inital work of each commit for different options

Adding Upstream

Add upstream as a remote, and configure it so you cannot push to it.

replace <upstream git repo> with the upstream repo url

example:

https://github.com/kubernetes/kubernetes.git git@github.com/kubernetes/kubernetes.git

git remote add upstream <upstream git repo>
git remote set-url --push upstream no_push

This can be verified by running git remote -v which will list your configured remotes.

Keeping Your Fork in Sync Fetch all the changes from upstream and “rebase” them on your local master branch. This will sync your local repo with the upstream project.

git fetch upstream
git checkout master
git rebase upstream/master
git checkout -b myfeature
git rebase master

You should do this minimally before creating a new branch to work on your feature or fix.

Squashing Commits The main purpose of squashing commits is to create a clean readable git history or log of the changes that were made. Usually this is done in last phase of a PR revision. If you are unsure if you should squash your commits, it is better to err on the side of having more and leave it up to the judgement of the other contributors assigned to review and approve your PR.

https://github.com/kubernetes/community/tree/master/contributors/guide/contributor-cheatsheet#Examples-of-GoodBad-Communication

https://gubernator.k8s.io/pr