This is my note for The Git & Github Bootcamp course
Git Core : Intro to Git, Installation, Command Line, Git Basics, Committing in Detail, Branching, Merging
Next Level Git : Diffing, Stashing, Undoing Changes
Github and Collaboration Core : Github Basics, Fetching & Pulling, Github Odds & Ends, Collaborative Workflows
The Other Parts : Rebasing, Interactive Rebasing, Git Tags, Git Behind The Scenes, Reflogs, Custom Aliases
- Configuration
# user's name
git config user.name # get
git config --global user.name "[name]" # set
# email
git config user.email # get
git config --global user.email "[email]" # set
# default branch-name
git config init.defaultBranch # get
git config --global init.defaultBranch main # set-
ls: list directory content -
ls -a: list all directory content including hidden one -
pwd: print working directory (return current working directory) -
cd: change directory (onlycdis same ascd ~) -
cd ..: back to parent directory -
cd /: go to terminal directory -
cd ~: go to user home -
cd c/folder: go to folder in drive c -
mkdir test: create folder -
start folder(Windows) : open folder in file explorer (open folderfor other OS) -
start .(Windows) : open current working directory in file explorer (open .for other OS) -
echo "" > index.html(Windows) : create index.html file in current working directory (touch index.htmlfor other OS) -
cat > b.txt+enter+hello+enter+crtl+v: create file withhelloin content -
cat b.txt: look at content -
cat b.txt >> c.txt: concatenate b.txt to c.txt -
subl: Open sublime text (Windows). follow config below first.-
add code below in file
C:\Users\<username>\.bash_profile -
alias subl="/c/Program\ Files/Sublime\ Text/sublime_text.exe"
-
-
open -a “Sublime Text”(Other OS) : open sublime text for other OS -
mv index.html about.html: change nameindex.htmltoabout.html -
mv style.css /css: movestyle.cssto/csssubfolder -
rm <file>: remove file -
rm -r <folder>: remove folder (-r recursive inside folder as well) -
clear: clear output -
cp (-r) <file/folder name> -r <folder>: copy file/folder use -r for folder (recursive) -
nano b.txt: open text editor
-
Repo (Repository) : workspace / folder
-
git status -
git init -
git add file1 file2 -
git add . -
Working Directory
-
Staging Area : Something intermediate between working directory abd repo
-
Repo (Repository) : workspace /
.gitfolder
flowchart LR
A(Working Directory) -->|git add| B(Staging Area)
B -->|git commit| C(Repository)
-
git commit -m "message" -
git commit -a -m "message"orgit commit -am "message":git add <tracked-file>andgit commit -m "message". Note:<tracked file>is not include new file. -
git log
-
Atomic Commit : 1 commit, 1 thing
-
Use present tense in Git Commit message (It is only convention in Git Docs)
-
Change default text editor for
git commitwithout message
git config --global core.editor "code --wait"
-
git log --oneline -
git commit --amend: change last commit
-
git branch: list all branches -
git branch <branch-name>: create new branch -
git switch <branch-name>: switch branch -
git checkout <branch-name>: switch branch -
git switch -c <branch-name>: create and switch branch -
git checkout -b <branch-name>: create and switch branch -
Commit before switch branch
-
git branch -d <branch-name>: delete fully merged branch -
git branch -D <branch-name>: force delete unmerged branch -
git branch -m <branch-name>: rename current branch to<branch-name>
- Merge
<incoming-branch>into<receiving-branch>. We have to be on the<receiving-branch>
git switch <receiving-branch>
git merge <incoming-branch>
- Fast-Forward Merge :
<receiving-branch>and<incoming-branch>are in the same chain.<receiving-branch>is in the past and will be fast forward to<incoming-branch>
- Non Fast-Forward Merge without Conflict :
<receiving-branch>and<incoming-branch>are in the different chains. Git will generate new commit consisting both chains from<receiving-branch>and<incoming-branch>.
- Non Fast-Forward Merge with Conflict :
- open files with merge conflicts
- remove the conflicts and remove conflict marker
git add .andgit commit
-
git diff: working directory and staging area -
git diff HEAD: working directory and HEAD (last commit) -
HEAD~1: parent of head -
git diff --staged: last commit and staging area -
git diff <filename1> <filename2>: narrow down to specific<filename1>and<filename2> -
git diff <branch-name1>..<branch-name2>:<branch-name1>and<branch-name2><old>..<new> -
git diff <commit-hash1>..<commit-hash2>:<commit-hash1>and<commit-hash2>(<commit-hash>is git hash, some pre-fix of git hash)
-
save uncommitted changes without unnecessary commits.
-
git stash: save all uncommitted change (staged and unstaged) -
git stash pop: pop oldest stash (FIFO) and re-apply to current working copy -
git stash apply: apply without remove -
git stash list: show stash queue (FIFO) -
git stash apply stash@{<id>},git stash apply stash@{2}: apply specific stash in the queue -
git stash drop stash@{<id>},git stash drop stash@{2}: remove specific stash in the queue -
git stash clear: clear all stash
git checkoutare old command that can do many thing but it is confusing. There are 2 new command for more specific thing:git switchandgit restore
flowchart LR
A(git checkout) --> B(git switch)
A --> C(git restore)
-
git checkout <commit-hash1>: go to specific commit (<commit-hash>is git hash, some pre-fix of git hash) -
Detached HEAD : Head point to specific commit instead of branch. (Normally HEAD point to branch)
-
git switch <branch-name>,git checkout <branch-name>: re-attaching head -
git checkout HEAD~1: reference relative to head, go to parent of head -
git checkout HEAD~2: reference relative to head, go to grandparent of head -
git switch -: go back to current commit of the branch -
git checkout HEAD <file>orgit checkout -- <file>orgit restore <file>: discarding changes in that files, reverting back to the head. -
git restore .: discarding all changes, reverting back to the head. -
git restore --source HEAD~1 <file>: git restore to 2 commit ago -
git restore --staged <file>: unstage file (move from staging area t working directory) -
git reset <commit-hash>: remove commit (but not remove change in working directory) -
git reset --hard <commit-hash>or -git reset --hard HEAD~1: remove commit and remove change
git revert <commit-hash>: similar togit resetbut not remove commit. it create new commit with undo change.
-
Github : host for git repositories
-
git clone <url>: download git repository to local machine -
git clone --branch <branchname> <remote-repo-url>: clone specific branch -
SSH keys : can connect github without identifying username and password
-
git remote -v: display list of remotes -
git remote add <name> <url>: add a new remote -
git remote rename <old-name> <new-name>: rename remote -
git remote remove <name>: remove remote -
git push <remote> <branch-name>: push<branch-name>on local to<branch-name>on remote -
git push <remote> <local-branch>:<remote-branch>: push<local-branch>on local to<remote-branch>on remote -
git push -u <remote> <branch-name>: push and makegit push=git push <remote> <branch-name>for next time usage
-
git branch -r: list all remote branches -
git branch -vv: list all local branches and their remote tracking branches -
git checkout <remote>/<remote-branch>: go to<remote-branch>on remote -
git switch <remote-branch>orgit checkout --track <remote>/<remote-branch>: make branch<local-branch>=<remote>/<remote-branch>in the same branch name
flowchart LR
A(Working Directory) -->|git add| B(Staging Area)
B -->|git commit| C(Local Repository)
C -->|git push| D(Remote Repository)
D -->|git fetch| C
D -->|git pull| A
git fetch <remote>: fetch all changes from the remote repository without merging to local working directory. this will not create conflict
-
git pull <remote> <branch>:git fetch <remote>+git merge <branch>- Good practice :
git pulland fix merge conflict beforegit push - can result in merge conflict
- not recommend if you have uncommitted changes!
- Good practice :
-
git pull: this will use default value asgit pull origin <tracking-current-branch>
-
Github Gists : are a simple way to share code snippets and useful fragments with others. Gists are much easier to create, but offer far fewer features than a typical Github repository.
-
Github pages : public webpages that are hosted and published via Github. Github Pages is a hosting service for static webpages, so it does not support server-side code like Python, Ruby, or Node. Just HTML/CSS/JS!
- Everyone Works On Master/Main
- The Most Basic Workflow Possible
- Problem: lots of conflicts, incomplete code in master.
- Treat master/main branch as the official project history
- all new development should be done on separate branches!
- Multiple teammates can collaborate on a single feature and share code back and forth without polluting the master/main branch
- Master/main branch won't contain broken code (or at least, it won't unless someone messes up)
- Pull Request : are a feature built in to products like Github & Bitbucket. They are not native to Git itself
- Forking : allow us to create personal copies of other peoples' repositories. We call those copies a "fork" of the original.
- forking is not a Git feature. The ability to fork is implemented by Github.
- I fork the original project repo on Github
- I clone my fork to my local machine
- I add a remote pointing to the original project repo. This remote is often named upstream.
- I make changes and add/commit on a feature branch on my local machine
- I push up my new feature branch to my forked repo (usually called origin)
- I open a pull request to the original project repo containing the new work on my forked repo.
- Hopefully the pull request is accepted and my changes are merged in!
-
Rebase :
- as an alternative to merging
- as a cleanup tool for git history (interactive rebase)
-
move
<side-branch>to the head of<main-branch>- all commits in
<side-branch>are recreate (commit hashes are changed)
- all commits in
git switch <side-branch>
git rebase <main-branch>
-
Never rebase commit that already shared with others.
-
git rebase --continue: conflict happen after rebase and fixed the conflict before continue rebase
-
rewrite, delete, rename, reorder commits using
git rebase(clean commit history) -
should rebase only working feature branch before sharing.
-
should not rebase shared commit!!
-
git rebase -i HEAD~4 -
common commands for rebase
- pick - use the commit
- reword - use the commit, but edit the commit message
- edit - use commit, but stop for amending
- fixup - (combine) use commit contents but meld it into previous commit and discard the commit message.
- drop - remove commit
-
pointer to specific commit.
-
most often used to mark version releases in projects (v4.1.0, v4.1.1, etc.)
-
Think of tags as branch references that do NOT CHANGE. Once a tag is created, it always refers to the same commit. It's just a label for a commit.
-
2 types
- lightweight tags : only name/label
- annotated tags : store extra meta data including the author's name and email, the date, and a tagging message (like a commit message)
-
Major.Minor.Patch (e.g., 2.4.1)
-
Initial Release (1.0.0)
-
Patch Release (1.0.1) :
- Patch releases normally do not contain new features or significant changes. - They typically signify bug fixes and other changes that do not impact how the code is used
-
Minor Release (1.1.0) :
- Minor releases signify that new features or functionality have been added, but the project is still backwards compatible. No breaking changes.
- The new functionality is optional and should not force users to rewrite their own code.
-
Major Release (2.0.0) :
- Major releases signify significant changes that is no longer backwards compatible.
- Features may be removed or changed substantially.
-
Document : semver.org
-
git tag: list all tags -
git tag -l "*beta*": search tags -
git checkout <tag>e.g.git checkout v17.0.0: go to<tag> -
git diff v17.0.0 v17.0.1 -
git tag <tagname>(lightweight tag) : tag HEAD -
git tag -a <tagname>(annotated tag) : tag HEAD -
git show <tagname>: show tag information -
git tag <tagname> <commit>: tagging previous commits -
git tag -f <tagname>orgit tag -f <tagname> <commit>: force tag- tag name must be unique. in case, it is not unique, git will return error that tag exists. and
-fwill force that tag to move to the commit
- tag name must be unique. in case, it is not unique, git will return error that tag exists. and
-
git tag -d <tagname>: delete tag -
git push --tags: push tags- normally
git pushnot push tags.
- normally
-
git push <remote> <tag>: push specific tags to remote
-
git config --local: local config file -
Git Object
- blob : binary large object (content in file)
- tree : folder
- commit
- annotated tag
-
nothing much important here!
-
please see original slides here
-
Reflog : Reference logs
-
git reflog show HEAD: show logs of head -
git reflog show <branch-name> -
git checkout HEAD@{2}: go to specific head log -
git reflog master@{one.week.ago} -
git checkout bugfix@{2.days.ago} -
git diff main@{0} main@{yesterday}
- Assign those alias in
~/.gitconfigorC:\Users\<username>\.gitconfig(global config file)
[alias]
s = status
l = log
cm = commit -m
-
git config --global alias.l log- make
git l==git log
- make
-
useful existing alias online
[alias]
ls = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat











