Skip to content

Instantly share code, notes, and snippets.

@sahal
Last active July 25, 2019 08:16
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 sahal/6a8b79a68a511e8b0df81a1eac192ab8 to your computer and use it in GitHub Desktop.
Save sahal/6a8b79a68a511e8b0df81a1eac192ab8 to your computer and use it in GitHub Desktop.

Notes

I saw that it was missing a few things, so I wrote this.

Temporarily (in this shell session) set your VISUAL env var:

  • It's okay if you don't want to use vim. This is a judgement free zone.
(ins)parsons@parson[~]$ export VISUAL=nano

To set these permanently, put them in your bash config. Alternatively, you can set git specific editor preferences -- Google is your friend here.

Let's clone a repository

(ins)parsons@parson[~]$ git clone
https://github.com/jessfraz/dockerfiles.git
Cloning into 'dockerfiles'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8543 (delta 2), reused 8 (delta 2), pack-reused 8532
Receiving objects: 100% (8543/8543), 72.11 MiB | 1.73 MiB/s, done.
Resolving deltas: 100% (3551/3551), done.

Move into it

(ins)parsons@parson[~]$ cd dockerfiles/

Branch off the default branch

  • In this case we know the default branch is master -- will be useful later
(ins)parsons@parson[~/dockerfiles]$ git checkout -b feature
Switched to a new branch 'feature'

Bonus: How to find the default branch

  • SHOCKER: It is /probably/ master, but maybe not if your team has a different branching strategy)
$ git remote show origin
* remote origin
Fetch URL: https://github.com/jessfraz/dockerfiles.git
Push URL: https://github.com/jessfraz/dockerfiles.git
HEAD branch: master
Remote branches:
imgbot tracked
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)

The line with the HEAD branch is the default branch.

Make an arbitrary change and commit it

(ins)parsons@parson[~/dockerfiles]$ rm wine/*
(ins)parsons@parson[~/dockerfiles]$ git status
On branch feature
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working
directory)
deleted: wine/Dockerfile
no changes added to commit (use "git add" and/or "git commit -a")
(ins)parsons@parson[~/dockerfiles]$ git add wine/Dockerfile
(ins)parsons@parson[~/dockerfiles]$ git commit -m "removed wine"
[feature ff616ea] removed wine
1 file changed, 17 deletions(-)
delete mode 100644 wine/Dockerfile

Make another arbitrary change and commit it

(ins)parsons@parson[~/dockerfiles]$ rm zsh/*
(ins)parsons@parson[~/dockerfiles]$ git add zsh/Dockerfile
(ins)parsons@parson[~/dockerfiles]$ git status
On branch feature
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted: zsh/Dockerfile
(ins)parsons@parson[~/dockerfiles]$ git commit -m "removed zsh"
[feature 6d4278e] removed zsh
1 file changed, 13 deletions(-)
delete mode 100644 zsh/Dockerfile

Realize that you made one too many commits and your co workers might not respect you for it.

(i'll wait)

Start an interactive rebase

  • We will specify the default branch -- which we know is master -- here -- but you probably want the branch you branched your feature branch from
(ins)parsons@parson[~/dockerfiles]$ git rebase -i master
  • You'll see the actual commits you're modifying here in your default VISUAL editor
pick ff616ea removed wine
pick 6d4278e removed zsh
# 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

I mostly only use pick, reword, or fixup. You might prefer squash to fixup -- the only difference being that squash keeps your commit message. I don't like that because I write a lot of shitty commit msgs while debugging.

So for each commit, you can change the "pick" part of the line to either reword or fixup. Note, you obviously can't merge the first commit with the one previous to it, so you really only can use the commands that don't do that.

Once you've selected the commits you want to reword or fixup, save the file using your editor's commands.

Now, git will take that file and rewrite your commit history.

Force push your branch to your origin

  • So it has your re-written history too
git push -u origin feature --force

This will rewrite history on your origin (GitHub, stash, Gitlab, w.e.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment