Skip to content

Instantly share code, notes, and snippets.

@RossBencina
Last active August 22, 2023 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RossBencina/e93e604f5302e758771064dc2f989482 to your computer and use it in GitHub Desktop.
Save RossBencina/e93e604f5302e758771064dc2f989482 to your computer and use it in GitHub Desktop.
Quick Reference for Common Git Operations

Quick Reference for Common Git Operations

Derived from: https://github.com/PortAudio/portaudio/wiki/UsingTheGitRepository

Official Git reference: https://www.git-scm.com/docs/


TLDR

git status

If your main branch is named master:

# sync local master branch with upstream repo master branch:
git checkout master
git pull origin
git fetch upstream master
git merge upstream/master
git push origin master

If your main branch is named main:

# sync local main branch with upstream repo main branch:
git checkout main
git pull origin
git fetch upstream main
git merge upstream/main
git push origin main
# make a new branch and develop on it:
git checkout -b <new branch name>

git add ...
git commit -m...

# first push:
git push --set-upstream origin <new branch name>
# or equivalently
git push --set-upstream origin HEAD
# or equivalently (needs recent git)
git push --set-upstream-to HEAD origin
# or, since git>=2.37.0, do this once:
git config --add --bool push.autoSetupRemote true
# then
git push origin

# subsequent pushes:
git add ...
git commit -m...

git push origin

Set up your fork for the first time

1. Fork the upstream project repo.

Use the GitHub website to fork the upstream project repo into your personal GitHub account.


Set up a local working directory for the first time

Prerequisite: Set up your fork for the first time above.

2. Make a local working directory by cloning your fork:

git clone https://github.com/{your_account}/portaudio.git
cd portaudio
ls

3. Set the upstream repo as an upstream repo on your local working directory:

git remote -v
git remote add upstream https://github.com/PortAudio/portaudio.git
git remote -v

Sync your local working directory with origin and upstream

Prerequisite: Set up a local working directory for the first time above.

0. cd to your local working directory

1. Sync your local working directory with origin (i.e. origin = your fork)

git checkout master
git pull origin

2. Sync your local working directory with upstream (i.e. upstream = official portaudio project repo)

git fetch upstream master
git merge upstream/master

3. Push the synchronised master back to your fork on GitHub

git push origin master

Work on a (new) Pull Request (PR)

Prerequisite: Sync your local working directory with origin and upstream above.

0. cd to your local working directory

1. Create a new branch off master and switch to that branch

git checkout master
git checkout -b {the name of your new branch}

2. Some useful commands are

git status
git diff # (see "Diff your changes" section below)
git add {file names}
git commit -m"your commit message"

(DYOR)

3. Push your committed changes to your fork on GitHub

The first time, use the command:

git push --set-upstream orgin {branch name}

GitHub will reply with a link for creating the PR.

Alternatively, since git>=2.37.0, do this once:

git config --add --bool push.autoSetupRemote true

Then you can push the first commit with:

git push origin

Then once you have set the upstream branch you can use the following for additional pushes:

git push origin

Note that if the upstream brach is not set, you can always do the following to push:

git push origin {branch name}

Manage branches in your local working directory

List branches:

git branch

Change to branch:

git checkout {branch name}

Delete a local branch:

git branch -D {branch name}

Diff your changes

Diff current work area with changes already staged for commit:

git diff

Diff staged against most recent commit to current branch:

git diff --staged

Diff current work area (both staged and unstaged) with most recent commit to current branch:

git diff HEAD

You can diff a specific file by adding the file name to the end of any of the above commands:

git diff {file name}

You can diff between branches (with optional file name):

git diff {branch name a} {branch name b}

and between commits:

git diff {commit hash a} {commit hash b}

Stashing

Save and restore uncommitted changes to the stash stack.

git stash [push]
git stash pop

https://www.git-scm.com/docs/git-stash


Amending

Add more files or changes to the most recent commit, with or without editing the commit message:

git add {file names}
git commit --amend -m "New and correct message"

or

git add {file names}
git commit --amend --no-edit

Amend only the commit message of the most recent commit:

git commit --amend -m "New and correct message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment