Skip to content

Latest commit

 

History

History

git

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Git

Resources

Config

aliases in ~/.gitconfig [alias]

[alias]
  sw = switch
  ci = commit
  st = status
  re = restore
  br = branch
  l = log --oneline --graph --decorate  --all
  pick = cherry-pick -n

Commands

Checkout is confusing

checkout was a confusing command, an has now mostly been replaced by other commands.

Instead of git checkout some_branch, use git switch some-branch

Instead of git checkout file_in_working_tree, use git restore file_in_working_tree

Instead of git checkout -b some_branch, use git branch some-branch -u origin/some-branch (but then have to sw into the branch)

git br docs-itertools-examples git push -u origin docs-itertools-examples

Forking a GitHub repo

Based on GitHub Fork a Repo docs and Merging an Upstream Repo into your Fork

Use the GitHub UI to fork the project, e.g., fork [email protected]:octocat/Spoon-Knife.git to [email protected]:octocat/Spoon-Knife.git. Then:

git clone [email protected]:YOUR-USERNAME/Spoon-Knife.git
git remote add upstream [email protected]:octocat/Spoon-Knife.git
git fetch upstream

If you already cloned from what you now want to be the upstream before forking:

git remote set-url origin [email protected]:YOUR-USERNAME/Spoon-Knife.git
git remote add upstream [email protected]:octocat/Spoon-Knife.git
git branch -D main

Then, create local branches tracking branches of the same name in the upstream:

git branch main -u upstream/main

# OR

git branch develop -u upstream/develop
git branch master -u upstream/master

Fetch a remote

The entire remote:

git fetch origin
git fetch upstream

only one branch of a remote:

git fetch origin main

Merge a remote to local

on main:

git merge origin/main

Rebase

Rebase a branch against it's parent, on the branch, run:

git rebase main

To interactively rebase (e.g., to squash)

git rebase -i HEAD~4

Reset

  • --hard -- resets both both index and working tree (e.g., trashes anything not committed)
  • --mixed -- resets the index but not the working tree
  • --soft -- leaves index and working tree as they are. Useful for getting some changes "out" of a branch to modify and re-commit.

Examples:

git reset --hard ${hash}
git reset --hard HEAD~4 # four commits before HEAD

Push

Force push to a remote. Dangerous if anyone else is using it!

git push --force origin my_branch

Rename branch locally and remotely

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Delete a branch locally and remote

git branch -d {branch_name}
git push {remote_name} -d {branch_name}

Stash

Show all the patch-diff format for a stash:

git stash show -p stash@{0}

Submodules

git clone --recursive 
git submodule init 
git submodule status
git submodule update

Hooks

Hooks are not part of a clone, but you can either (1) put the scripts in the repo and require users to symlink to them or (2) add them to the Template Directory

.git/hooks/pre-commit is a local hook run prior to committing. to bypass, use git commit --no-verify

.git/hooks/pre-receive is a server hook run prior to accepting a push

Clean branches that don't exist on the remote

git fetch --prune

Reflog

Lost a commit some how? check the reflog ("reference log")

Bisect

Useful for finding a breaking change if you can easily validate a bug against it.