新建代码库
1 | # 在当前目录新建一个Git代码库 |
配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。1
2
3
4
5
6
7
8
9# 显示当前的Git配置
$ git config --list
# 编辑Git配置文件
$ git config -e [--global]
# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
增加/删除文件
1 | # 添加指定文件到暂存区 |
代码提交
1 | # 提交暂存区到仓库区 |
分支
1 | # 列出所有本地分支 |
标签
1 | # 列出所有tag |
查看信息
1 |
|
远程同步
1 | # 下载远程仓库的所有变动 |
撤销
1 | # 恢复暂存区的指定文件到工作区 |
其他
1 | #生成一个可供发布的压缩包 |
Git Basics
- git init Initialize a repository
- git status Show status of working tree
- git add file.txt Start tracking file.txt
- git add main.txt Stage modified file main.txt
- git diff Show what’s changed but not yet staged
- git commit Commit changes
- git commit -a Stage files and commit
- git mv main.txt file.txt Rename main.txt to file.txt
- git fetch develop Pull data from remote ‘develop’ without merging
- git pull origin develop Fetch and merge branch ‘develop’ from origin
- git clone url Create local copy of remote repository at ‘url’
Branching
- git branch Show current branches
- git push origin master Push master branch to origin server
- git branch -v Show last commit on all branches
- git checkout master Switch to branch ‘master’
- git branch feature1 Create new branch called ‘feature1’
- git checkout -b feature2 Create branch ‘feature2’ and switch to it
- git branch -d mybranch Delete branch ‘mybranch’
- git branch –merged Show branches already merged into current branch
- git branch –no-merged Show branches not yet merged into current branch
- git branch -D fix Force delete branch ‘fix’ that is not yet merged
- git push origin feature1 Push local branch ‘feature1’ to origin
- git push staging develop:master Push develop branch to remote staging master
- git checkout -b fix1 origin/fix1 Create local branch ‘fix1’ based off origin branch
- git checkout –track origin/fix2 Create tracking branch ‘fix2’ based off origin
- git push origin :fix2 Delete remote branch ‘fix2’ from origin
`
Merging / Rebasing
- git mergetool Use graphical merge tool
- git commit Finalize merge after resolving conflicts
- git merge feature1 Merge branch ‘feature1’ with current branch
- git add file.txt Mark file.txt as resolved after merge
- git rebase develop Rebase changes made on current branch over develop
- git rebase master develop Rebase master onto develop without checking it out
- git rebase –onto master 1a 1b Rebase master onto branch 1b made from branch 1a
Remotes
- git remote Show remote servers you have configured
- git remote -v Show remote servers with URL displayed
- git remote add myurl url Add remote server ‘url’ with shortname ‘myurl’
- git remote rename server1 server2 Rename remote ‘server1’ to ‘server2’
- git remote rm server1 Remove remote ‘server1’
- git remote show origin Show info about remote origin
Commit Logs
- git log Show commit logs
- git log -p -2 Show last two commits with diffs
- git log –stat Show commit logs with stats
- git log –pretty=oneline Show commit logs one per line
- git log –graph Show commit logs with ascii graph
- git log –since=1.week Show commit log for the last week
- git blame -L 10,15 file.rb Show prev commits for each lines 10-15 of file.rb
Undo / Change History
- git rm –cached main.txt Remove main.txt from staging but keep in working
- git commit –amend Replace last commit with whats in staging
- git checkout – file.txt Discard changes to file.txt
- git reset HEAD file.txt Unstage file.txt
- git commit –amend Modify last commit message
- git rebase -i HEAD~3 Make changes to the last 3 commits
Using Tags
- git tag Show available tags
- git tag -a v3.0 Create annotated tag ‘v3.0’
- git show v3.0 Show info for tag v3.0
- git tag -s v3.0 Create signed tag v3.0
- git tag v2.1-lw Create lightweight tag v2.1
- git tag -v v3.0 Verify signed tag v3.0
- git tag -a v2.2 8feb Tag previous commit ‘8feb’ as v2.2
- git push origin v2.2 Push tag v2.2 to origin
- git push origin –tags Push all local tags to origin
Using Stashes
- git stash Stash changes without committing
- git stash list Show stores stashes
- git stash apply Reapply most recent stash
- git stash apply stash@2 Reapply stash 2
- git stash apply –index Reapply stashed changes along with staged changes
- git stash drop stash@{2} Drop stash 2
- git stash pop Apply most recent stash and drop from stack
- git stash branch mybranch Create branch ‘mybranch’ from stash
- git stash clear Delete all stashes
- git diff –staged Show what’s staged but not yet committed
- git diff –check Check for whitespace errors before committing
Using Bisect
- git bisect start Start binary search of commits to find bad commit
- git bisect bad Mark current commit as broken during bisect
- git bisect good v2.2 Mark v2.2 as last known good commit during bisect
- git bisect good Mark current commit as good during bisect
- git bisect reset Reset HEAD when finished with bisect
- git bisect run test.sh Run ‘test.sh’ on each commit during bisect