Skip to content

Instantly share code, notes, and snippets.

@ccdle12
Last active December 15, 2019 21:35
Show Gist options
  • Save ccdle12/3112c9ba023a5a26212f4a0618b0b075 to your computer and use it in GitHub Desktop.
Save ccdle12/3112c9ba023a5a26212f4a0618b0b075 to your computer and use it in GitHub Desktop.

Git Cheat Sheet

Adding a submodule

A submodule is a github project within a github project. This has the benefit of using another github project code in a main project.

git submodule add <uri> local-path

Add to previous commit message

Adding to the previous commit without a new commit message

$ git add <some-file>
$ git commit --amend

Amend previous commit message

When you would like to change the previous commit message in history.

$ git commit --amend

Change remote origin

$ git remote set-url origin git@github.com:<some-user>/<some-project>.git

Creating a fork and tracking

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git
cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
git pull upstream master

Delete branch locally

First command deletes a branch that is not merged. Second command force deletest the branch CAREFUL.

$ git branch -d <name-of-branch>
$ git branch -D <name-of-branch>

Git reset a squashed commit

If you want to undo a squashed commit.

Shows the git command history.

git reflog

Resets the history to the position number displayed in git reflog this should be before the squash

git reset --hard HEAD@{5}

Merging Master instead of Rebase

This a good technique to use in teams that need to work on the same branch.

$ git checkout master
$ git pull master
$ git checkout feature
$ git merge master

Previously I was using git rebase master and force pushing updates. The problem with this approach is that it breaks the history for other developers working on the same branch.

Track and Fetch another users branch

Add the user and their repo.

git remote add <name-of-user> <address-of-user-github-repo>

Fetch the users branches.

git fetch <name-of-user>

Checkout the users branch

git checkout --track <name-of-user>/<branch>

Install submodules

When first pulling a project, you need to install all the submodules.

git submodule init
git submodule update

Remove locally commited commits

When you need to remove commits that are added and committed locally but not pushed to the repository.

NOTE: the needs to be the one hash BEFORE the commit you want to revert.

$ git reset <previous-commit-hash>

Working on branch that depends on another dev branch

This is a scenario where a developer is working on feature_a that is branched off master.

The developer is waiting for that branch to be merged but needs to start working on feature_b. But feature_b depends on the work of feature_a.

So this is a way for the developer to start working on feature_b while feature_a is being reviewed and eventually be able to merge to master without any complications.

  1. Checkout feature_a
$ git checkout feature_a
  1. Create the feature_b branch off feature_a and check it out.
$ git branch feature_b
$ git checkout feature_b
  1. Whenever feature_a changes while working on feature_b, pull the changes to feature_b
$ git checkout feature_b
$ git rebase feature_a
  1. Once feature_a is merged, do a final special rebase to 'graft' feature_b on to master, this means that feature_b is a branch that is fully branched off master and no longer dangling of feature_a.
$ git checkout master
$ git pull master
$ git checkout feature_b
$ git rebase --onto master feature_a feature_b

View changes in a git commit by hash

git show --color --pretty=format:%b aa737f6ab8a2b03810d04508a0a88980ca952c56

precommit hooks

link

  1. pip install pre-commit
  2. create a file named .pre-commit-config.yaml
  3. SAMPLE .pre-commit-config.yaml - runs the black formatter before commiting
repos:
-   repo: https://github.com/psf/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.6
  1. Install the precommit hooks
$ pre-commit install

Pull a remote branch from repo

When needing to pull a remote branch from the repo locally.

git pull origin <name-of-remote-branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment