Skip to content

Instantly share code, notes, and snippets.

@szeitlin
Created June 22, 2021 22:06
Show Gist options
  • Save szeitlin/6199c8dff4f6a29953193342096a5034 to your computer and use it in GitHub Desktop.
Save szeitlin/6199c8dff4f6a29953193342096a5034 to your computer and use it in GitHub Desktop.
Git tricks

Some references:

https://github.com/k88hudson/git-flight-rules

https://ohshitgit.com/

https://dangitgit.com/en

Some best practices

Stay on the “golden path” and you’ll be safe. Your usual workflow should be something like this:

  1. git pull origin master #make sure you have the latest version of master
  2. git checkout -b <newbranchname> #make a fresh branch off master
  3. Make sure the .gitignore is up to date. Always exclude passwords in config files, and any data files, especially large files - they’re hard to remove if you accidentally commit them.
  4. Make changes to your code, then use git status to check which files have changed. You may find more files have changed that you expected, for example if black or isort changed the formatting.
  5. Use git add <filename>.
  6. Write a useful commit with `git commit -m “”
  7. Push it up with git push
  8. Get a code review
  9. Once your PR is approved, squash commits and merge in github.

Things that might go wrong

If you committed a file you didn’t want to commit, but don’t want to lose your changes, check the git log and move the HEAD pointer back a commit or several: reset head --soft or

If you need something from another branch:

Just grab the file: git checkout <branchname> <filepath>

Or if you want to grab commits, use cherrypick

How to resolve a merge conflict

  1. Open the affected file and use your IDE to resolve. This assumes you know which parts to keep. If you don’t:

  2. My favorite option is “ours” vs. “theirs” for whole files: git checkout --ours <filepath>
    git checkout --theirs <filepath>

  3. Run the tests (this will happen automatically on git push once we have everything set up with Jenkins). This is the best way to confirm that you didn’t break anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment