Skip to content

Instantly share code, notes, and snippets.

@hmlimkokhoe
Last active December 8, 2022 11:07
Show Gist options
  • Save hmlimkokhoe/6156740888d822dba13f1731b1989d2c to your computer and use it in GitHub Desktop.
Save hmlimkokhoe/6156740888d822dba13f1731b1989d2c to your computer and use it in GitHub Desktop.
Useful commands for docker and git.

Docker

  • Create a Docker image: docker build -t my-tag .
    • -t my-tag Give your image a tag "my-tag"

git

  • Revert last commit but keep changes: git reset --soft HEAD~1
  • Revert all changes: git reset --hard
  • Create and switch to new branch but keep changes: git checkout -b new_branch

Branch switching

  • Switch branch from a branch that has changes: git checkout -f revert any non-commit change

  • Update your branch with remote branch, e.g. master:

  1. git reset --hard origin/master or git stash if you want to save your changes
  2. git pull

Merging strategies

The branch you're currently working on is referred to as HEAD. Git highlights merge conflicts in the source code by tagging the pulled commit with a commit hash, while the branch you're working on is identified as HEAD.

Merge according to GitLab

Step 1. Fetch and check out this merge request's feature branch:

git fetch origin
git checkout -b 'uiwi-ads-form' 'origin/uiwi-ads-form'

Step 2. Review the changes locally. Step 3. Merge the feature branch into the target branch and fix any conflicts. How do I fix them?

git fetch origin
git checkout 'dev'
git merge --no-ff 'uiwi-ads-form'

Step 4. Push the target branch up to GitLab.

git push origin 'dev'

Merge some branch myBranch to master using git rebase

  1. (master) git pull origin master or (master) git fetch --all --tags
  2. (my-branch) git rebase -i master
  3. (my-branch) git checkout master
  4. (master) git merge income_quiz
  5. (master) git push or (master) git push --force-with-lease

Maintenance and clean-up

  • Update local repository to match origin repository: It updates all branches according to the origin, also removing those not found in the latter. git fetch --all --prune

  • Remove all local branches not found in origin: git branch --v | grep "\[gone\]" | awk '{print $1}' | xargs git branch -D Source: https://stackoverflow.com/a/59228595

  • Rewrite history by squashing the latest N commits:

  1. git reset --soft HEAD~N && git commit Replace N with the number of the latest commits you want to squash. Source: https://stackoverflow.com/a/5201642 Or:
  2. git rebase -i --root - this allows you to squash all commits saved on the branch
  3. (interactive mode) Squash all commits you want to squash (squash works by merging to each previous commit; see snippet below)
  4. (interactive mode) Write an appropriate commit message for the squashed commits

All commits marked with "squash" are squashed with commit 066fb7a.

pick 5e96767 feat: CXO-1616 fix responsiveness, CXO-1617 implement feiertag theme
pick ad92bad refactor: CXO-1637 change minHeight to height for Quiz Widget
pick 066fb7a test: Enable logging to check payload
squash 16672eb refactor: Change variant declaration by fetching variant_name key
squash 562c394 refactor: Change api payload for test files
squash 0763c11 refactor: Fixed logic to correctly map variant
squash a49a84c fix: Fixed storybook not working after variantName change
pick d2e6b57 feat: CXO-1646 tiktok design

Stash

  • Save multiple git stashes, and apply a particular stash. Stashes are saved in a queue: Index 0 has the newest stash, whereas any subsequent one is an older stash. git stash list - View all stashes. git stash clear - Remove all stashes. git stash apply stash@{1} - Apply particular stash. In this case, the second stash element from the beginning of the queue.

Example:

stash@{0}: WIP on master: 5be8c11 feat: CXO-1660
stash@{1}: WIP on CXO-1666_Krieg: 5be8c11 feat: CXO-1660
stash@{2}: WIP on CXO-1666_Krieg: ae049d7 feat: loki - Visual Regression Testing for Storybook
stash@{3}: WIP on master: 5d26463 refactor: logrocket disabled
stash@{4}: WIP on CXO-1640: 4b0fdbf Fix: Fixed storybook not working after variantName change

Helpful non-git tools

Show history

  • history
  • history -c if you want to clear your history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment