Git — CheatSheet
Switch between branches:
git checkout branch-to-switch-on
Create new branch:
git checkout -b your-new-branch
Commit with message in editor:
It will open editor in your terminal for commit message.
Also, don’t forget to
git add what-ever-you-want
before committing.
git commit
Commit with an inline message:
git commit -m "Add commit message example"
Add remote:
git remote add origin https://your-git-host.something/repo.git
Update remote:
Well, trying using add
. It will tell you remote already exists. But try it anyways.
git remote set-url origin https://your-another-git-host.something/repo.git
Push changes to remote:
origin
is remote here. It can be anything, e.g. giddyup, ameriyuckcano etc..
git push origin branch-you-want-to-push
Push with force:
So, you have updated/amended your local branch and deleted/amended your commits, git
will not let to push
it unless you pull
the remote branch and merge changes.
But, you are smart and want to tell git who is the boss, use:
git push origin branch-you-want-to-push-with-force -f
OR
git push origin branch-you-want-to-push-with-force -force
Smartness can be expensive if you force push on
development
ormaster
.
Create a tag:
git tag tag-name
Useful for keeping track of releases, e.g. Release-101:
git tag Release-101
Delete a local tag:
git tag -d tag-name
Delete a remote tag:
git push --delete origin Release-195
Push a tag to remote:
git push origin tag-name
Fetch all tags from remote:
git fetch --tags
Push all tags to remote:
git push --tags
Merge Feature branch to Development:
git checkout development
git merge feature
Rebase Feature branch on Development:
git checkout feature
git rebase development
Rebase vs. Merge:
Rebase; Anna and Arthur were standing in a queue to buy sweet yummy soup, Max and Wayne just arrived and joined the queue behind Anna and Arthur.
Anna — ArhturMax — Wayne
Queue becomes:
🍲 Anna — Arthur — Max — Wayne [Head] 🚂
Merge; Anna and Arthur were standing in a queue to buy another sweet yummy soup, Max and Wayne just arrived and pretended they are friends with Arthur.
Anna — ArhturMax — Wayne
Queue becomes:
🍲 Anna — Arhtur - 💥Friends [Head] 👭
Max — Wayne - /
Map local branch to remote:
Instead of fetching all the branches, you created them locally, now you want to map local ones to remote (lets not talk about other issues like merging etc..), e.g mapping local
development
to remote
:
git branch — set-upstream-to=origin/development development
//be patient, more will come.