Skip to content

Instantly share code, notes, and snippets.

@KyleAure
Last active August 20, 2019 22:08
Show Gist options
  • Save KyleAure/1c98a2877578e3467f14e3168336bfef to your computer and use it in GitHub Desktop.
Save KyleAure/1c98a2877578e3467f14e3168336bfef to your computer and use it in GitHub Desktop.
Github Command Notes

Github Notes

Notes for some of the common but complicated things I do in git. All in one place so that I can copy and paste them!

Commit Triangle

When setting up a new local repo follow these steps to create a triangle that makes pull requests and other 'GitHub' specfic tasks easier to handle.

# This is the main repo such as git@github.com:OpenLiberty/open-liberty.git
git clone [repo]

In GitHub fork the repo you want to work on

# forked-repo is YOUR fork such as git@github.com:KyleAure/open-liberty.git
git remote add my_fork [forked-repo] 

# Set git to PUSH to your fork
git config remote.pushdefault my_fork 
git config push.default simple 

# Ensure that you now have origin and my_fork set up
#   my_fork	git@github.com:KyleAure/open-liberty.git (fetch)
#   my_fork	git@github.com:KyleAure/open-liberty.git (push)
#   origin	git@github.com:OpenLiberty/open-liberty.git (fetch)
#   origin	git@github.com:OpenLiberty/open-liberty.git (push)
git remote -v 

# Checkout master branch locally
git checkout -b master origin/master 

# Get any updates and ensure you are up to date
git pull 
git push

Create a New Branch

Used for creating a new branch with special settings if desired

# Switch to master or integration branch if you are not already there.
git checkout [master] 

# Make sure master is up to date with origin
git pull 

# Checkout a new branch.  
# Existing-branch is optional if you want to branch off of something other than master
git checkout -b {issue-num}-[branch-name] {existing-branch} 

Amend Commit

Commands to amend a commit when the changes are so small that they do not deserve their own commit. Also useful when making review changes so it looks like you got it correct the first time :D.

# Add files like usual by replacing file-name with the file you want to add
# Optionally just use -u for all tracked files or . for all files.
git add [file-name] {-u} {.} 

# Commit your added files an an amend
git commit --amend

# Push amendments to origin
# Optional -f if rewritting history
git push {-f} 

# Check that you status is "up-to-date"
git status

Logging changes

Useful for seeing what is happening with your repo and where your current branch/commit lies

# For a tree view of where your local changes lie with origin
git log --all --graph --decorate --oneline --simplify-by-decoration

# For seeing commits made to origin
# Optionally you can add how many days you want to go back
git log origin/master --author=kyleaure {--since=10days}

Rewritting History

Useful for situations where you want to combine local commits into one when pushed to origin

# Get the latest changes to origin
git fetch

Now we are goinng to use an interactive rebase. Rebase rewinds your local commits, moves your brach up to where master is now. Then attempts to re-play your commits on top. The {-i} tag makes this interactive so that before the re-play you can make changes to your commits.

git rebase origin/{master} -i

The interactive interface looks like this:

pick xxxx
pick xxxx
pick xxxx

# Rebase e227d91..7268d61 onto e227d91 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

This will allow you to rewrite history by squashing, editing commit messages, etc. Once you have chosen how you want to edit history press esc and then type :wq

At this point if there are merge conflics (which is always a posibility when rebasing) you will need to resolve those. After the rebase is finished:

# Attempt a regular push first.  Since we are rewriting history --force will most like be necessary.
git push {-f}

SSH-Agent

When pushing or pulling from github you will need to enter your SSH passphrase every time. To stop this you have a couple of options:

  1. You can add your ssh key as an identity by running the following command:
#Adds ssh idenitity for current terminal session
ssh-add ~/.ssh/id_rsa
  1. You can add your ssh key as an idenity to your keychain by running the following command:
#Persists ssh idenity between terminal sessions

#For Mac
ssh-add -K ~/.ssh/id_rsa

#For Linux
ssh-add -k ~/.ssh/id_rsa

Remote vs Local

Checkout remote branch

Whenever a remote branch is created it is also tracked on local in the remotes/origin namespace. So if a coworker creates a branched named test and pushes it to remote.
The next time you pull/fetch the test branch will be tracked locally. This branch will not show up as an active branch locally unless you check it out by doing the following:

git checkout -b [branch-name] origin/[branch-name]

Deleting a branch

When you merge a branch on origin you have the option to delete the branch. A branch that is deleted on origin is not automatically deleleted on local. To delete one branch on local do the following:

#If branch has been deleted on origin, and origin has been pulled
git branch -d {branchname}

#Otherwise, you will get a warning and need to use
git branch -D {branchname}

Pruning

If you work with many branches and want to cleanup your local repo you can use a command called prune. This will update your local workspace by deleting any branches that have been deleted on origin that are in the remotes/origin namespace. Note that prune will NOT delete branches on local that are currently being tracked (worked on). So your branches that have yet to be pushed and those that have been checked out are safe. To purne do the following:

git fetch --prune

If you want git to prune everytime you run git pull or git fetch you can change the prune settings for your repo by doing the following:

# This setting is only for this repo
git config remote.origin.prune true

This setting can also be set global and system wide:

# Set automatic pruning for all of YOUR git repos
git config --global fetch.prune true

# Set automatic pruning for all git repos on the system
# This is set to true automatically on linux systems which you may have noticed
git config --system fetch.prune true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment