ElectricRCAircraftGuy / ElectricRCAircraftGuy.github.io

My github pages website at gabrielstaples.com
https://gabrielstaples.com/
5 stars 1 forks source link

Write a new article titled "Git workflow examples and commands" #77

Open ElectricRCAircraftGuy opened 1 year ago

ElectricRCAircraftGuy commented 1 year ago

keywords: git cheatsheet cheat sheet, etc.

Include all of the standard workflow commands I use. Ex:

Standard beginner workflow

git status
git checkout main
git pull
git branch gstaples_feature1
git checkout gstaples_feature1

# make changes

git difftool
git add -A
git commit
git push -u origin gstaples_feature1

# make more changes

git difftool
git add -A
git commit
git push

# open pull request online

# once merged, pull new latest `main` and delete old feature branches (local and
# remote), and begin another branch for your next feature
git checkout main
git pull
git branch -d gstaples_feature1     # delete old local branch
git push origin :gstaples_feature1  # delete old remote branch
git branch gstaples_feature2
git checkout gstaples_feature2

Standard intermediate workflow (identical in function to the above)

This simply replaces a few commands above where 2 or more commands can be replaced by 1 single command instead. So, the function is identical to the function above, but it's more efficient is all. This is closer to what I normally do.

git status
git fetch origin main:main
git checkout -b gstaples_feature1 main

# make changes

git difftool
git add -A
git commit
git push -u origin gstaples_feature1

# make more changes

git difftool
git add -A
git commit
git push

# open pull request online

# once merged, pull new latest `main` and delete old feature branches (local and
# remote), and begin another branch for your next feature
git fetch origin main:main
git checkout -b gstaples_feature2
git branch -d gstaples_feature1     # delete old local branch
git push origin :gstaples_feature1  # delete old remote branch

Resolve merge conflicts by "rebasing (merge-style) onto latest main"

Beginner:

git status
git checkout main
git pull
git checkout gstaples_feature1
git branch gstaples_feature1_BAK_20220102-1300hrs_about_to_merge
git merge main
# manually resolve conflicts in each file
git add -A
git merge --continue
git status
git push

Intermediate:

git status
git checkout gstaples_feature1
git fetch origin main:main
git branch gstaples_feature1_BAK_20220102-1300hrs_about_to_merge
git merge main
# manually resolve conflicts in each file
git add -A
git merge --continue
git status
git push

Regardless of which method you choose above, you can now see only your gstaples_feature1 branch commits like this. Read ^main as not on branch main. If you just do git log, you'll see other peoples' upstream (and now merged into your branch) commits intermingled with yours. This can be very confusing and distracting.

git log ^main HEAD

Other tools

  1. git lg - https://coderwall.com/p/euwpig/a-better-git-log
  2. See my answers: many links at the bottom of my answer here (https://stackoverflow.com/a/63418267/4561887) and here (https://stackoverflow.com/a/65258783/4561887)
  3. Markdown Viewer in Chrome: https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk
  4. Markdown Viewer in Edge: https://microsoftedge.microsoft.com/addons/detail/markdown-viewer/cgfmehpekedojlmjepoimbfcafopimdg
  5. git changes
  6. git blametool
  7. git branch_
  8. etc.
  9. https://stackoverflow.com/a/69472178/4561887
    1. Teach about 2 vs 3 dots: git difftool main...HEAD vs git difftool main..HEAD
    2. git cherrypick
    3. previous commits: main~, main~2, main^, etc.
ElectricRCAircraftGuy commented 1 year ago

See #76 too.

See also my answer: https://stackoverflow.com/a/71508399/4561887