git help <command>
get docsgit clone <repo-link>
to create local copy of remote repogit status
to check the staging area & more before add/commit/pushinggit add <file>
to add to staging area-A
to add all changes, probably bad practice though
git commit -m <message>
to add messagegit push
to push to github-f
to force push ONLY USE WHEN REBASING--set-upstream origin <branch-name>
to create new upstream branch
git log
to view log of commits - q to exitgit pull
to update local filesgit stash
to append current commit in a stack & switch to last commitpop
applies the latest in the stackapply
to apply the stash but leave it in the stackdrop
to delete the most recent in the stackdelete
to delete the whole stack
git checkout <branch-name>
to move branch-b
to make new branch- To checkout a previous commit, use git log and checkout the long commit hash (e.g.
git checkout e32172ac35d1fa0f0270236d53bf2bddc617ffa4
) checkout <file-name>
resets the file to the previous commit
git branch
to see branches-a
to see remote branches too-m <new-name>
renames the current branch
git fetch
to get all new commits & update list of remote branchesgit merge <dev-brange-name>
to merge branches- You typically should be on the main branch
git rebase <new-base>
to rebase current branch--continue
to continue the rebase after fixing merge conflicts-i
is an interactive rebase see below
git reset --soft <commit-hash>
revert to commit & stash all changes upto this point- REMEMBER
--soft
to stash changes. This is a change of history! Avoid using!
- REMEMBER
git revert
appends a new commit that undoes the previous - this is not a change of historygit diff
shows difference between current files & latest commit-staged
compares staged changes & latest commit
git cherry-pick
takes a commit (usually from another branch) and append it to the current working HEADgit bisect
runs a binary search to isolate where a breaking change has been implemented - see below
Create a remote on github, a local directory & run the following
$ git init
$ git add <new-files>
$ git commit -m "init"
$ git remote add origin <remote-link>
$ git push -u origin main
$ git add <files>
$ git commit -m "updates"
$ git push
$ checkout <branch-to-rebase>
$ `rebase <new-base>
Fix conflicts
$ rebase --continue
And repeat
Branch B was branched off A. To rebase branch B onto main:
$ git checkout main
$ git pull
$ git checkout A
$ git pull
$ git checkout B
$ git rebase A
$ git rebase --onto main A
Opens a text editor (vim, nano, etc.) in order to carry out a rebase. The top lines are the instructions for a rebase, in the order that they will be applied. For example
<command> <hash> <commit-message>
pick 1558262 Update README.md
Below this is a list of commands and a short explanation. The important ones are
pick
uses the commit as it was initally appliedreword
changes the commit message onlyedit
pauses the rebase at this point to allow edits to the codesquash
like pick, but squashes this commit into the previous
$ git bisect start
$ git bisect good <commit>
$ git bisect bad <commit>
Bisecting: n revisions left to test after this (roughly n steps)
[hash] <commit message>
This begins a binary search, to isolate the commit where a bug appears. At each step declare either good or bad & a new commit will be provided to test
Adapted from this guide
Firstly create two github repositories repo-name
and private-repo-name
As normal set up main
to track the public repo
$ git remote add origin git@github.com:[user]/repo-name.com.git
$ git push -u origin main
Then set up the dev
branch to track the private repo
$ git checkout -b dev
$ git remote add private git@github.com:[user]/private-repo-name.com.git
$ git push -u private dev
This will give one local repo with two branches that push to different remote repos.
When you make a PR on github, a merge commit is created at the ref refs/remotes/pull/PR-NUMBER/merge
on origin. In order to access this you can
- Pull the refs with
git fetch origin +refs/pull/*:refs/remotes/origin/pr/*
- Checkout the commit at
origin/pr/PR_NUMBER/merge
Github Actions will use this commit by default, so any CI tests will be running on this commit, not your branch.