My Notes on Git

December 4, 2023 - Reading time: ~1 minute

Stuff I keep forgetting

Work on a remote branch

  1. git fetch <origin> <remote_branch>
  2. git checkout -b <local_branch_name> <origin>/<remote_branch>
    1. can also be: git checkout <remote_branch>

Track a remote branch

  1. git checkout -b <local_branch> # if no local created yet
  2. git branch -u <origin>/<remote_branch>
Will update this as needed.

Script to fix git commit author and email

January 8, 2022 - Reading time: ~1 minute

I don't remember where I found this but it's quite useful.

#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Sync a git Repo with Upstream

April 21, 2021 - Reading time: ~1 minute

Steps to add a remote repo to a local project and sync with it.

  • git remote add upstream # Add the upstream as a remote
  • git fetch upstream # Fetch the upstream
  • git checkout master # Checkout brach to sync with upstream
  • git merge upstream/master # Sync upstream's master branch locally

Push an Existing Project into GitHub

April 8, 2021 - Reading time: ~1 minute

I always forget these steps so noting them here. This does assume that an empty repo is first created on GitHub.

  • git init # initialize directory to be a new git repo
  • git add . # add files
  • git commit -m "Initial Commit" # commit the files
  • git remote add origin # add the GitHub repo as the remote
  • git remote -v # verify addition
  • git push origin master # push commit to master branch on GitHub

Categories