Jon Rumsey
An online markdown blog and knowledge repository.
Project maintained by nojronatron
Hosted on GitHub Pages — Theme by mattgraham
Git Cheatsheet
Keep a record of learned GIT tricks and traps.
Checkout Workflow
CSharp Fritz used this flow on his Twitch stream one day:
- Checkout a new branch:
git checkout -b {branch-name}
- Set upstream to remote:
git push -u origin {branch-name}
- Stage changed files:
git add {...filename | .}
- Commit staged changes and add a comment:
git commit -m '{50-char-un-truncated-comment}
- Push changes to remote:
git push
Key Takeaways:
- Use remote tracking by using the
-u
flag to simplify future push commits by using just git push
- If remote tracking is not enabled, use
git push {remote-name} {branch-name}
but make sure you get the branch-name correct.
Set Upstream
Two common ways:
- When setting up a new branch.
- When adding a remote branch to your local.
Set Upstream To New Branch
git checkout -b {branch-name}
git push -u origin {branch-name}
- Start deving and git flowing.
Add Remote Branch To Local
git checkout -b {remote-branch-name}
git branch --set-upstream-to={remote}/{remote-branch-name} {remote-branch-name}
For example:
- A common trunk branch named "staging" exists remotely.
- Your remote repository is named "origin".
- You need a local copy of staging branch to weed out merge conflicts before creating a PR.
git checkout -b staging
git branch --set-upstream-to=origin/staging staging
Tagging
- Set a tag at the current commit:
git tag {new-tag-name}
- Push tag(s) to remote:
git push upstream main
Key Takeaways:
- Name a Tag at any commit using the command line.
- Pushing a Tag to remote does not require opening a new branch or PR.
Git Reset
Usage example git reset --{option} {commit_hash}
:
Options:
- Soft: Removes the last commit from current branch, retaining file changes
git reset --soft HEAD~1
- Mixed: Same as soft except retains changes in working tree but not in the index (unstaged)
git reset --mixed HEAD~1
- Hard: Loose all uncommitted changes, including untracked files. This effectively rolls-back the commit and all file changes to the previous commit
git reset --hard HEAD~1
- Merge: Undo a merge wile preserving uncommitted changes in current working directory.
- Keep: Similar to Hard but uses a diff against all files so uncommitted changes are not reset.
Return to ContEd Index
Return to Root README