🕌
Cheet sheet to handle with Git in everyday development
Basic Flows
Create Repository
Create a remote branch in Github before you push a new repository from local
// initialize
mkdir <my_project>
cd <my_project>
git init
git remote add origin <remote_repository_URL>
// after addition or modification of files
git add <file_name>
git commit -m "<commit_message>"
git push -u origin main
Team Development
// create branch
git checkout <parent_branch>
git pull <parent_branch>
git checkout -b <feature_branch>
// modifiy file as you need
git add .
git commit -m "<commit_message>"
git push -u origin <feature_branch>
// (optional)
// in the case your branch got outdated and you can't merge in the develop
git rebase <parent_branch>
git push -f
// after modification
git checkout <parent_branch>
// (optional)
// you would do this in GitHub
git merge <feature_branch>
git push origin --delete <feature_branch>
// after merge
// clean up local branch
git branch -d <feature_branch>
Frequent modifications
Change branch name
git branch -m <old_branch_name> <new_branch_name>
Amend commit message
// Current commit
git commit --amend
// Past commit
// Change status pick -> reword
// Then automatically open commit message editor
git rebase -i HEAD~n // amend n commits from latest
git rebase -i <commit_id> // start ammend from latest to <commit_id>
Trouble shooting
Back to specific commit
// move to specific commit
// cannot reflect your modification (detached HEAD)
git checkout <commit_id>
Stash Change
git stash list // show all stash list
git stash show -p stash@{n} // show nth stash detail
git stash push "<stash_message>" // save changes to stash
git stash apply stash@{n} // apply nth stash
git stash drop stash@{n} // delete nth stash
git stash clear // clean up all stash
Conflict
Commonly usable among branch or stash
- Roll back without resolving conflicts
git restore --source=HEAD --staged --worktree .
git merge --abort // merge
git rebase --abort // rebase
- Resolve conflicts
git status
// Edit conflicted files
git add <conflicted-file>
git commit
Commit History Management
- To modify commit detail
// for resent commits
// role back nth commit reference before
git reset --mixed HEAD~n // changes unstaged
// edit changes
git push
// for far past commits
git rebase -i
// change `pick` to `edit` for target commits
git rebase --continue
// edit files
// need -f option to push
git push -f
- To delete commit
// permanently delete local change and write over by remote branch
git fetch origin
git reset --hard origin
// pick multiple commits to delete
git rebase -i HEAD~{n}
// change `pick` to `drop` for target commits
git rebase --continue
// need -f option to push
git push -f
Recovery
- Backup Branch
// create backup_branch
git checkout <feature_branch>
git checkout -b backup_<feature_branch>
// work on feature_branch
git checkout <feature_branch>
// failture something like rebase or reset --hard
// recovery from backup_branch
git reset --hard backup_<feature_branch>
git push -f
- Last resort
Avoid messing up anymore before searching by reflog
// Find
git reflog
// get commit id to roll back
git reset --hard <commit id>
Detail Differences
Staging
git add . // All of the modification
git add <file_name> // Specific files
Push
git push -u origin <feature_branch> // First time. Set the link between remote and local
git push <feature_branch> // May omit -u option after setting tracking branch
Pull
git fetch // Update remote tracking branch. Not merge to local branch.
git pull // Update local current branch
git pull <branch_name> // Update specific local branch
Reset
git reset --soft HEAD~n // changes on staging
git reset --mixed HEAD~n // changes unstaged
git reset --hard <commit_id> // changes deleted
Research
- Branch
git branch // show all local branch
git status // show staging status in current branch
*Reference History
git reflog
- Commit History
git log // show commit log
git log --oneline // in one liner
git log --author=<author_name> // someone specific
git log --since="yyyy-mm-dd" --until="yyyy-mm-dd" // in specific term
git show <commit_id> //Detail of a specific commit
git blame <file_path> // show who edit the line
- File Difference
git diff // all
git diff <file_name> // specific file
git diff <commit_id> <file_name> // between specific commit and working tree
git diff <commit_id_1> <commit_id_2> // between specific commits
Discussion