Git and GitHub
Git: Commands
- Create Repo
git init
initial a folder as a new repo, tracking all modification under it.git/config
configure only for this repo
git clone <path> [<new dir name>]
- cannot create nested repo, so do check your
pwd
- cannot create nested repo, so do check your
git status
- Review Commits
git log
for commit historygit log --oneline
show only 7 characters of SHA and commitgit log --stat
show files changed for each commitgit log --patch
orgit log -p
show the detailed changes of each commitgit log -w
ignore the change of blank when showing patch infogit log <7 character of SHA>
start from particular commit
git show
show commit with knowing SHA- share all arguments of
git log
- share all arguments of
- Make Commits
git add
git add .
to stage all folders and filesgit rm --cached <file>
to unstage
git commit
open default editorgit commit -m "commit message"
for short commit with only message- Rules of writing commits
- Message: explain the role of submission
- Good: “Update the footer to copyright information”
- no why / no how / no and
- Description: skip a line after message then start
- Message: explain the role of submission
git diff
show changes of unstaged files.gitignore
file under repo to ignore specified files
- Develop on Branches
git tag
show taggit tag -a v1.0 [<SHA>]
create a tag “v1.0” with comments (creator / date / message) for (optional) particular commitgit tag -d "v1.0"
remove tag “v1.0”- use
git log
to verify the location of tag (use--decorate
in older version)
git branch
show all branchesgit branch <branch_name> [<SHA>]
create a new branch at (optional) particular commitgit branch -d <branch_name>
delete branch (inactivate) w/o commit- to delete branch with commits, use
-D
instead
- to delete branch with commits, use
git checkout
switchHEAD
pointer to a branch, making it as active branch- convert file version to latest version of active branch (it replace all files under commit in previous branch, with that in the new branch)
git checkout -b <new_branch_name> <create_on_which_branch>
create a new branch and checkout it
git log --oneline --graph --all
shows all branches and commits, also relationshipsgit merge
git merge <other_branch>
merge a branch into current branch, likemaster
- Fast-forward: moving
HEAD
to the latest commit (mergesocial-links
tomaster)
- Auto-merge: create a commit and merge (merge
sidebar
tomaster
) - Merge conflicts: same row has been differently modified in both branches
- git stop merging and return a single file, combining conflicting portions from both files, showing conflicts with indicators
- update the file and commit it, then merge is completed automatically
- you can just commit it and let merge done, but the conflicts will show in your file
- Fast-forward: moving
git reset --hard HEAD^
cancel merge
- Undo Changes
git commit --amend
alter the most -recent commitgit add
and rungit commit --amend
to add new files into last commit
git revert <SHA>
create a new commit reversing given commit- Commit Reference
git reset
erases commitgit reset --mixed HEAD^^
moveHEAD
back toHEAD^^
, and move changes ofHEAD^
into working dir (default)git reset --soft HEAD^^
move changes back to stagegit reset --hard HEAD^^
delete changes- Backup strategy
- before any
reset
operation,git branch backup
- after reset, if want to back to the starting point
git checkout -- index.html
remove uncommited change to file “index.html”git merge backup
- before any
Git: Shell Setting
GitHub: Remote Repo
git remote
show shortname of remote repogit remote -v
show detailed infogit remote add <shortname> <repo_link>
add in repo link and its shortname
git push <remote_shortname> <remote_branch>
push all your local git info to remote repo- after
push
,git log
will show the commit location of remote-tracking branch
- after
git fetch <remote_shortname> <remote_branch>
download remote info and move remote-trackinggit pull <remote_shortname> <remote_branch>
fetch and merge with local branch
GitHub: Collaborating with Fork
git shortlog
show commits under different collaboratorgit shortlog -s -n
show the number of commits only, sorted by large to small
git log --author="Paul Lewis"
show commits made by Paul Lewisgit log --grep="bug"
- How to contribute?
Fork
the repo- Check CONTRIBUTION.md
- To contribute large effort, use
Issues
to communicate with project maintenance before starting - Create branch with clear name meeting requirement
- Create clear, frequent and small commits
- Pull request
GitHub: Keep Updated to Remote Repo
-
Pull Request
-
Fetch and Pull from Upstream
-
git rebase
transfer commits onto a new basegit rebase -i HEAD~3
squash last three commits interactively (refer to YouTube for detailed setting)- need force
git push
due to deleting commits
- need force
- Backup strategy: create backup branch before
rebase