Global Information Tracker (git)

Git commands I like to remember.

Put Aside and Get Back

git stash puts uncommitted changes in a special commit to retrieve later. When you have some work that is not yet ready to commit, but you need to somethein else to the repo or get the fresh remote changes.

  • git stash save if you have uncommitted work.
  • git pull --rebase get updates from the remote and apply them
  • git stash pop restore your uncommitted work.

Select Hunks Interactively

With git add -p <file> for patch mode the file or files are not just added but you can choose which so called hunks of the changes should staged for commiting.

...
(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]?
  • y accepts the hunk to be staged.
  • n declines the hunk to be staged.
  • s tries to split the hunk up into smaller pieces.
  • a accepts the hunk and any remaining.
  • d declines the hunk and any remaining.

Forgott a Thing

To change your last commit and add something or fix a typo in the commit messag, use --amend:

git commit -m "initial commit"
git add the_file.txt
git commit --amend

You can now overwrite your previous commit and have just one clean commit.

Roll back the remote

You need the desired commit somewhere in your local repo. You can find it with git log.

git push --force origin <commit-ish>:<remote branch>

commit-ish can be the commit hash or a tag.

Add Forked Branch

Adding a forked branch to the actual project is helpful to pick or patch the main branch.

git remote add forked https://github.com/someone-else/something.git
git fetch forked
git checkout -b somethingBranch forked/main

The checkout command creates a local branch that tracks the remote branch somethingBranch on remote forked.

git branch

  main
* somethingBranch