origin/main
by checking the output of
git status
git 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 schannel
blog
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 status
blog
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 --force
git rebase --abort
and
merge insteadA bad rebase or merge might leave users feeling confused and like they’ve “messed up” their repo
Here’s a general process for sorting out a mess:
git log --graph --all --oneline
git status
Here’s a quick cheat sheet of useful (but potentially dangerous) commands that can learn more about to help you sort out Git messes:
git cherry-pick
: Replay a single commit on top of the
current branchcommit --amend
: Instead of making a new commit, rewrite
the last onegit rebase --interactive
: Rewrite a branch by deleting,
editing, or re-ordering commitsgit reset
: Force a branch to point to a different
commitgit 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 restore
Fork
main
, make each entry in
a new branch, and merge them into main
with pull
requests