commit 06b6b9d1f4ee9d50dc197a369a3933f48f533aee parent 37089070e42e6065b63ed6d94b1bc3cf353a4522 Author: Yohanes Bandung <bandungpenting@gmail.com> Date: Sat, 16 May 2020 17:17:43 +0700 content: remove specific file from git commit Diffstat:
| M | assets/content.scss | | | 18 | ++++++++++++++---- |
| A | content/posts/Remove Specific Files from Old Git Commit/1.png | | | 0 | |
| A | content/posts/Remove Specific Files from Old Git Commit/2.png | | | 0 | |
| A | content/posts/Remove Specific Files from Old Git Commit/3.png | | | 0 | |
| A | content/posts/Remove Specific Files from Old Git Commit/4.png | | | 0 | |
| A | content/posts/Remove Specific Files from Old Git Commit/index.md | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | layouts/_default/_markup/render-image.html | | | 5 | ++--- |
| M | layouts/_default/single.html | | | 6 | +++++- |
8 files changed, 100 insertions(+), 8 deletions(-)
diff --git a/assets/content.scss b/assets/content.scss @@ -51,6 +51,11 @@ padding: 0.1em; font-family: $font-mono; font-size: 0.9em; + &.language-bash { + &:before { + content: "$ "; + } + } } :not(pre) > code { @@ -90,9 +95,11 @@ ol { margin: 1.5em 0; padding: 0; - list-style: none; - counter-reset: my-awesome-counter; - li { + counter-reset: my-awesome-counter, foot-note-counter; + :not([role="doc-endnote"]) { + list-style: none; + } + li:not([role="doc-endnote"]) { counter-increment: my-awesome-counter; list-style-position: outside; margin-bottom: 0.2em; @@ -101,7 +108,7 @@ &:before { position: absolute; right: 0.5em; - top: 0.25em; + top: 0.1em; content: counter(my-awesome-counter)'.'; font-size: 0.8em; @include themify($themes) { @@ -110,6 +117,9 @@ } } } + li[role="doc-endnote"] { + counter-increment: foot-note-counter; + } } ul { diff --git a/content/posts/Remove Specific Files from Old Git Commit/1.png b/content/posts/Remove Specific Files from Old Git Commit/1.png Binary files differ. diff --git a/content/posts/Remove Specific Files from Old Git Commit/2.png b/content/posts/Remove Specific Files from Old Git Commit/2.png Binary files differ. diff --git a/content/posts/Remove Specific Files from Old Git Commit/3.png b/content/posts/Remove Specific Files from Old Git Commit/3.png Binary files differ. diff --git a/content/posts/Remove Specific Files from Old Git Commit/4.png b/content/posts/Remove Specific Files from Old Git Commit/4.png Binary files differ. diff --git a/content/posts/Remove Specific Files from Old Git Commit/index.md b/content/posts/Remove Specific Files from Old Git Commit/index.md @@ -0,0 +1,79 @@ +--- +title: Remove Specific Files from Old Git Commit +author: Yohanes Bandung Bondowoso +description: "Ever accidentally committed <code>node_modules</code>? +Or want to remove files from a commit 15 before <code>HEAD</code>? +I did. I'd like to share the solution for you." +tags: + - git #git + - tutorial #tutorial +date: 2020-05-16T13:01:19+0700 +image: + src: 1.png +ID: 1589608880 +--- + +tldr; [Skip to solution ยป](#how-tos) + +Last week, I encountered a merge conflict while trying to sync my git working branch to latest remote `master`. Merge conflicts is not a serious problem to me, I am used to resolving conflicts. But the conflict that I encountered is from an auto generated typings file from `Apollo Codegen`[^1]. + +`Apollo Codegen` file is `Flow` or `TypeScript` typings for the schema and queries in project that uses GraphQL. The generated file often has >10k lines of code. Trying to resolve conflict in that file will make the text editor unresponsive (even with Vim!). + +So I aborted the sync master (`git rebase --abort`), then attempt to remove the changes for the auto-generated codegen file [using lazygit](#lazygit-way). Afterwards, I do sync remote `master` branch and the conflict don't happen. Last thing I do is regenerate the codegen file before posting a Pull Request. + +## How-Tos + +Back to the post's main topic, to remove specific file using plain old shell command. + +First, checkout to **temporary branch** with the afore mentioned commit as `HEAD` using the commit's hash: + +```bash +git checkout <commit-hash> +``` + +Then do a **soft reset** to uncommit with all files in staged status: + +```bash +git reset --soft HEAD^ +``` + +Make the desired file(s) unstaged using **reset** command, and then commit with `-c ORIG_HEAD` flag to use the previous commit message. The `--no-edit` flag is optional. + +```bash +git reset <path/to/file> +``` +```bash +git commit -c ORIG_HEAD --no-edit +``` + +Discard the changes of the file you want to remove from unstaged area: + +```bash +git checkout -- . +``` + +Last, `rebase` this **temporary branch** to your branch, from the commit of `<commit-hash>`. + +```bash +git rebase --onto HEAD <commit-hash> <destination-branch-name> +``` + +If you do this for already merged Pull Request, you need to `push <remote> <branch> --force`. Mind you, doing this will be inconvenient for other people working on same project. + +## Lazygit Way + +As I mentioned above, I use [lazygit](https://github.com/jesseduffield/lazygit). The process is more straight forward. For the following example, I accidentally committed build file, far before I `.gitignore`d `build/` folder. What I do is: + + + + + + + + + +Do this way if you want to install lazygit before doing your intention. + +`lazygit` way is easier. I also use [tig](https://github.com/jonas/tig) as a TUI for `git`, but I don't know the command to do the steps wit `tig`. + +[^1]: Here is `Apollo Codegen` [Github](https://github.com/apollographql/apollo-tooling#apollo-clientcodegen-output) page. As a frontend engineer, I consider `codegen` as breakthrough because I can easily type the endpoint's return value. diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html @@ -60,7 +60,7 @@ data-srcset="{{ $src_set }}" width="{{ $image.Width }}" height="{{ $image.Height }}" alt="{{ $alt }}" /> </a> <figcaption> - <em>{{ .Text }}</em> + <em>{{ .Text | safeHTML }}</em> </figcaption> </center> -</figure>- \ No newline at end of file +</figure> diff --git a/layouts/_default/single.html b/layouts/_default/single.html @@ -23,7 +23,11 @@ </div> <br> <br> - <p {{ if eq .Type "page" }}class="hcard"{{ end }}><a class="u-url link-white" href={{ .Permalink }}>Published</a> on <time title="{{ .Date.Format "Monday, 02 January 2006 on 15:04:05" }} GMT+7" class="dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05" }}+07:00">{{ .Date.Format "02 January 2006" }}</time>, for stories like this:</p> + <p {{ if eq .Type "page" }}class="hcard" hidden=""{{ end }}> + <a class="u-url link-white" href={{ .Permalink }}>Published</a> on + <time title="{{ .Date.Format "Monday, 02 January 2006 on 15:04:05" }} GMT+7" class="dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05" }}+07:00"> + {{ .Date.Format "02 January 2006" }}</time>, + for stories like this:</p> {{ with .Params.tags }} <p class="taglist"> {{ range . }}