Mastering Git

Terminology
working directory local directory
staging area between working directory and repository
repository remote
remote branch created by others

Configurations
Set up workspace on Windows
Set up SSH key
git config --global user.name "Name"
git config --global user.email "Email"
git config --global push.default upstream
git config --global merge.conflictstyle diff3
git config --global color.ui auto
git config --global --edit -> [alias] (\n) st=status

Commands

git local machine

git diff id1 id2 compare two commits
git diff compare the files in the working directory(local) to the staging area
git diff --staged compare the files in the staging area and the repository
git reset --hard discard any changes in the working directory or the staging area
git reset [file] Unstages the file but preserve its contents
git checkout id checkout a commit
git init initiate a git repository
git log see git commit history
git log -n num see a specific number of log histories
git status
git branch [branch-name] create a new branch
git checkout [branch-name]switches to the specified branch and updates the working directory Notice the difference with git checkout id
git checkout -b create a new branch and checkout it
git log --graph --oneline [branch1] [branch2] to see commit history of two branches, so that we can compare time, etc
git log only displays reachable commits
git merge [branch] combines the specified branch's history into the current branch
git merge [branch1] [branch2] merge two branches AND CURRENT BRANCH, so we should always checkout to one of the two branches if we only want them to merge
git show show the diff between a commit and its parent
git branch -d [branch-name] delete the specified branch label but reserve the commits
git remote add origin HTTPS/SSH.git add remote url
git remote -v see what is your remote
git push [remote] [local branch]
git pull = git fetch + git merge
git stash temporarily stores all modified tracked files
git stash pop & git stash apply restores the most recently stashed files
git checkout -- <file> to discard changes in working directory
git rebase <branch_rebased_on> <cur_branch> rebase the brach, if the second parameter is provided, we will checkout the branch after rebasing, otherwise it will checkout <brach_rebased_on>
git cherry-pick <commit> first create a new branch, then cherry-pick a commit you want to extract. if there is conflict, you can resolve the conflict, and then do git cherry-pick --continue


git reflog shows all commits that have been checkout recently
git branch lists all local branches in the current repository
git rm [file] delete the file from the working directory and stages the deletion
git rm --cached removes the file from version control but preserves the file locally
git mv [file-original] [file-renamed] changes the file and prepares it for commit
git reset [commit] undoes all changes after [commit], preserving changes locally
git reset --hard [commit] discards all history and changes back to the specified commit
git stash list lists all stashed changesets
git stash drop discards the most recently stashed changeset

Error messages/Warnings

error: Your local changes to the following files would be overwritten by checkout:
game.js
Please, commit your changes or stash them before you can switch branches.
Aborting
  1. Use git diff to see your files to the most recent commit.
  2. Use git reset --hard to get rid of the changes you made. This command cannot be undone.
Auto-merging game.js
CONFLICT (content): Merge conflict in game.js
Automatic merge failed; fix conflicts and then commit the result.
  1. Resolve the conflict
  2. If you run "git status", you would see "both modified"
  3. git add
  4. git commit

Git ignore example

*.json

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Others
.DS_Store
p2_482.xcodeproj/
p2_482/
p2/
p2.xcodeproj/

Reference
Git Cheatsheet
Udacity Course: How to Use Git and Github
1
2
3
4

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容