Git with local repos

  • git add: Git to track our file
  • git diff <filename>: show only the unstaged changes by default. If wish to show the difference between staged and µcommitted, used --stage.
  • git commit -a -m <your msg>: skipping staging and go directly to commit, but this does not work on new files because they are untracked. -a means to add all known files.
  • git log --all --oneline --decorate --graph shows the log
  • git show <commit id> show details of a specific commit
  • git add -p p for patch. Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.
  • git rm <filename> stops a file being tracked and removing from the directory. changes are staged to be committed.
  • git reset HEAD <filename> unstages added changes
  • git mv <filename1> <filename2> to rename file in the repo. This will also change the working directory.
  • git merge --abort discard merge
  • git rebase <branch name>: move the current branch on top of the . This will help keep the history linear

Git remote command

  • git remote: list remote repositories
  • git remote update: git remote update can update all of your branches set to track remote ones, however not merge any changes in
  • git fetch: git fetch can update only the branch you are on, however not merge any changes in
  • git pull: can update and merge any remote changes of the present branch you are on
  • git branch -r: List remote branches

How to undo changes

reset vs checkout

  • reset resets the index without touching the working tree
  • checkout changes the working tree without touching the index.

Several usage scenarios

git checkout <filename>
  • When to use: stage –> Working
  • Will change working directory: yes
  • How it works: checkout a file from repo so that the last changes are discarded
    This will revert changes to modified files before they are staged.
git checkout head <filename>
  • When to use: stage –> stage
  • Will change working directory: Yes
  • How it works: checkout a file from repo so that the last changes are discarded
    This will revert changes to modified files before they are staged.
git reset
  • When to use: repo –> stage
  • Will change working directory: No
  • How it works: unstage changes, reset commit
git reset –-hard origin/master
  • When to use: remote repo –> local repo –> working
  • Will change working directory: Yes
  • How it works: after committing to remote repo

How to modify a previous commit

Fix a (fixable) commit

Scenario: The previous commit misses either some file or the msg is not correct

Solution: use --amend along with staged changes to modify the previous commit

What it actually does: overwrite the previous commit

Heads up: avoid using it on public repos

1
2
3
4
git add a.py
git commit -m 'added two files' # wrong commit
git add b.py
git commit --amend # this will also open an editor to modify the previous commit msg.

revert a (bad) commit

Scenario: roll back a bad commit

Solution: revert the commit

What it actually does: creates an inverse of the previous commit

1
2
git revert HEAD # revert the previous head
git revert <commit ID> # revert another commit back in time

How to squash commits when doing pull request

Use this when forking and doing pull requests back to the master.

  1. rebase to master. We can use rebase since typically only one person uses the forked repo

    1
    git rebase -i master # I for interactive
  2. squash commits

    • make the first commit
      1
      2
      3
      4
      5
      git checkout -b new_branch
      edit
      git add .
      git commit -m 'first commit'
      git push
    • make the second commit
      1
      2
      3
      4
      edit
      git add .
      git commit -m 'second commit'
      git push # now new_branch is ahead of master by two commit
    • rebase and squash. If you mark one or more lines as “squash”, they will be combined with the one above
      1
      2
      git rebase -i master #interactively squash commits
      git show # show the latest commit

Miscellaneous

cherry-pick

  • use when selecting commits from other branches into main branch
  • git cherry-pick <commitID1> <commitID2>

modify old commits

  • use git rebase -i to reorder the commit I want to change to be on top
  • use git commit --amend to modify the commit
  • use git rebase -i again to change the order back

tag

  • use when I want some anchor more permanent than a branch or commit
  • git tag <versionid> <commitID>
  • use git describe <a reference> to describe where you are relative to the closest “anchor”. The reference could be a branch

    relative referencing

  • Moving upwards one commit at a time with ^
    1
    2
    git checkout main^
    git checkout main^^
  • Moving upwards a number of times with ~<num>
    1
    git branch -f main HEAD~3 # reset main 3 commits upwards
    Screen Shot 2021-01-07 at 5 49 42 PM

Reference:

  1. https://bugfender.com/blog/robust-development-with-git-flow-bitbucket-pipelines-and-bitrise/
  2. https://www.coursera.org/learn/introduction-git-github