Mark's MarkDown
  • notes
    • elevator pitch
    • cs
      • languages
        • elixir
          • data pipelines
            • broadway kafka
            • broadway
          • features
            • tree of contents
          • tips
            • enum
            • elixir tips
        • git
          • git notes

Tree of Contents

  • notes
    • elevator pitch
    • cs
      • languages
        • elixir
          • data pipelines
            • broadway kafka
            • broadway
          • features
            • tree of contents
          • tips
            • enum
            • elixir tips
        • git
          • git notes
Source
Résumé

Home

2024-03-14
Git Tips
[all notes]

Commands

Renaming the Current Branch

git branch -m new-branch-name

Reverting Commits

  • → reverses changes without erasing the commit from history
  • → …a safe and non-destructive way to correct errors or unwanted alterations in your project
    • → for instance, imagine 3 commits; the middle one has undesirable changes…
git revert commit_hash_2
  • → Git will create a new commit, let’s call it Commit 4, which negates the changes introduced by Commit 3.
  • → Commit 4 becomes the latest commit in your branch, and the project now reflects the state it would have been in if Commit 2 had never happened.

Un-stage a File

  • → removes a file from the staging area, allowing you to make additional modifications before committing
    git reset filename
    
    # use `git reflog` to get a list of commit hashes

Reset Commit(s)

git reset --soft HEAD^

Restore

# Unstage changes made to a file, same as "git reset some-file.py"
git restore --staged some-file.ex

# Unstage and discard changes made to a file, same as "git checkout some-file.ex"
git restore --staged --worktree some-file.ex

# Revert a file to some previous commit, same as "git reset commit -- some-file.ex"
git restore --source HEAD~2 some-file.ex

Amend → Update the Last Commit

  • → forget to include a change in your last commit? …use this command to add to the previous commit
    • → keep in mind that if you have already pushed the commit to a remote repository,
  • → you’ll need to force push the changes using
  • → git push --force to update the remote branch because a standard git push operation appends a new commit to your remote repository rather than modifying the last commit.
    git commit --amend -m 'message'

Switch

  • → only switches branches, git checkout does much more
  • → performs extra sanity checks that checkout doesn’t, for example switch would abort operation if it would lead to loss of local changes.
    git switch other-branch
    git switch - #last branch
    git switch remote-branch

Worktree

git branch
# * dev
# master

git worktree list
# /.../some-repo  ews5ger [dev]

git worktree add -b hotfix ./hotfix master

# Preparing worktree (new branch 'hotfix')
# HEAD is now at 5ea9faa Signed commit.

git worktree list
# /.../test-repo         ews5ger [dev]
# /.../test-repo/hotfix  5ea9faa [hotfix]

cd hotfix/  # Clean worktree, where you can make your changes and push them

Bisect - new in Git 1.7.14

git bisect start
git bisect bad HEAD  # Provide the broken commit
git bisect good 479420e  # Provide a commit, that you know works
# Bisecting: 2 revisions left to test after this (roughly 1 step)
# [3258487215718444a6148439fa8476e8e7bd49c8] Refactoring.

# Test the current commit...
git bisect bad  # If the commit doesn't work
git bisect good # If the commit works

# Git bisects left or right half of range based on the last command
# Continue testing until you find the culprit

git bisect reset  # Reset to original commit

Basic Concepts

HEAD

  • → HEAD is a special pointer/reference that always points to the latest commit in the current branch
  • → When you make a new commit, HEAD moves forward to point to that new commit
  • → …if you’re on the main branch and you make a new commit, HEAD will now point to that new commit, indicating that it’s the most recent one in the main branch

^

  • → ^ serves as a means to navigate through your project’s historical timeline
  • → Using HEAD^ references the commit immediately preceding your current one
  • → If you append a number following ^, such as HEAD^2, it references the second commit preceding(before) your current commit
  • → ^ allows you to traverse backwards in your project’s history and the numerical argument allows you to move back to a specified number of commits

Reference Links

  • → 10 must know git commands
  • → modern git commands
  • → Delete a git commit but keep the changes? - SOF
  • → Force git pull - SOF