origin/main by checking the output of
git statusgit config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor nano
git config --global init.defaultBranch main
# For Linux or macOS:
git config --global core.autocrlf input
# For Windows:
git config --global core.autocrlf true
# If you're behind a corporate proxy on Windows
git config --global http.sslBackend schannelblog repository:mkdir blog
cd blog
git init
echo "# Entry 1" > entry_1.md
git add entry_1.md
git commit -m "Add first blog entry"
git statusblog repository on GitHub, and
pushing your local repo to it:git graph alias for visualising branches:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch feature
checkout feature
commit id: "X"
commit id: "Y"
checkout main
commit id: "C"
commit id: "D"
Problem: feature conflicts with
main, how do I update feature so that it will
merge cleanly back into main?
Option 1: git merge main
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch feature
checkout feature
commit id: "X"
commit id: "Y"
checkout main
commit id: "C"
commit id: "D"
checkout feature
merge main
feature multiple timesOption 2: git rebase main
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature (old)"
checkout "feature (old)"
commit id: "X"
commit id: "Y"
checkout main
commit id: "C"
commit id: "D"
branch "feature (rebased)"
checkout "feature (rebased)"
commit id: "X*"
commit id: "Y*"
X and Y are “replayed” on top of
main
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature (old)"
checkout "feature (old)"
commit id: "X"
commit id: "Y"
checkout main
commit id: "C"
commit id: "D"
branch "feature (rebased)"
checkout "feature (rebased)"
commit id: "X*"
commit id: "Y*"
feature, Git will stop you from
overwriting the “old commits” unless you push with
git push --forcegit rebase --abort and
either:
A bad rebase or merge might leave users feeling confused and like they’ve “messed up” their repo:
origin/feature, you
fetch it and make commits in your local feature
branch:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "origin/feature" order: 1
checkout "origin/feature"
commit id: "X"
commit id: "Y"
branch "feature" order: 2
checkout "feature"
commit id: "Z"
checkout main
commit id: "C"
commit id: "D"
origin/feature, meaning your push will now fail:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature" order: 2
checkout "feature"
commit id: "X"
commit id: "Y"
commit id: "Z"
checkout main
commit id: "C"
commit id: "D"
branch "origin/feature" order: 1
checkout "origin/feature"
commit id: "X*"
commit id: "Y*"
origin/feature, but
instead of rebasing, you merge
git merge origin/feature:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature" order: 2
checkout "feature"
commit id: "X"
commit id: "Y"
commit id: "Z"
checkout main
commit id: "C"
commit id: "D"
branch "origin/feature" order: 1
checkout "origin/feature"
commit id: "X*"
commit id: "Y*"
checkout "feature"
merge "origin/feature"
git reset Z to get
back to the state prior to the merge:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature" order: 2
checkout "feature"
commit id: "X"
commit id: "Y"
commit id: "Z"
checkout main
commit id: "C"
commit id: "D"
branch "origin/feature" order: 1
checkout "origin/feature"
commit id: "X*"
commit id: "Y*"
git rebase origin/feature:
%%{init: { 'gitGraph': {'showCommitLabel': true} } }%%
gitGraph
commit id: "A"
commit id: "B"
branch "feature (old)" order: 2
checkout "feature (old)"
commit id: "X"
commit id: "Y"
commit id: "Z"
checkout main
commit id: "C"
commit id: "D"
branch "origin/feature" order: 1
checkout "origin/feature"
commit id: "X*"
commit id: "Y*"
branch "feature" order: 3
checkout "feature"
commit id: "Z*"
git graph (aka
git log --graph --all --oneline)git statusHere’s a quick cheat sheet of useful (but potentially dangerous) commands that you can learn more about to help you sort out Git messes:
git cherry-pick: Replay a single commit on top of the
current branchgit commit --amend: Instead of making a new commit,
modify the last onegit rebase --interactive: Rewrite a branch by deleting,
editing, or re-ordering commitsgit reset: Force a branch to point to a different
commit
--soft, --mixed, and
--hard options determine what happens to your working
directory and staging areagit restore: Update files in the working directory with
content from a specific commitgit filter-repo:
Power tool for rewriting history
git gc)git reflog lists commits that
HEAD has pointed to recently
git checkout, while we’ve used the modern
git switch and git restoreForkmain, make each entry in
a new branch, and merge them into main with pull
requests