Creating a repository: git init
Create a new local Git repository in a new subdirectory named GitInPracticeRedux
1 2
cd /Users/mike/ git init GitInPracticeRedux
Building a new commit in the index staging area: git add
Add an existing file GitInPractice.asciidoc to the index staging area for inclusion in the next commit.
1 2
cd /Users/mike/GitInPracticeRedux/ git add GitInPractice.asciidoc
Committing changes to files: git commit
Commit the contents of an existing file GitInPractice.asciidoc, which has already been added to the index staging area.
1
git commit --message 'Initial commit of book.'
Viewing history: git log
View the commit history of a repository.
1
git log
Viewing the differences between commits: git diff
View the differences between the previoius commit and the latest.
1 2 3
git diff master~1 master git diff master~1 master --GitInPractice.asciidoc ## 只比较此文件 git diff # compare any changes you've made but not yet added with git add.
Remote Git
Adding a remote repository: git remote add
Add the new GitInPractice remote repository to your current repository.
Pushing changes to a remote repository: git push
Push the changes from the local GitInPracticeRedux repository to the origin remote on GitHub.
1
git push --set-upstream origin master
Cloning a remote/GitHub repository onto your local machine: git clone
Remove the existing GitInPracticeRedux local repository and re-create it by cloning from GitHub.
1 2 3
cd /Users/mike/ rm -rf GitInPracticeRedux git clone https://github.com/username/GitInPracticeRedux.git GitInPracticeRedux
Pulling changes from another repository: git pull
Pull new commits into the current branch on the local GitInPracticeRedux repository from the remote repository on GitHub.
1
git pull
Fetching changes from a remote without modifying local branches: git fetch
git fetch performs the fetching action of downloading the new commits but skips the merge step.
1 2
git fetch git pull # pull after fetch
Creating a new local branch from the current branch: git branch
Create a new local branch named chapter-two from the current(master) branch.
这个命令创建一个新分支,但并不切换到这个分支上,要切换还是要用git checkout。
1
git branch chapter-two master
Checking out a local branch: git checkout
Change to a local branch named chapter-two from the current(master) branch.
切换分支前,如果当前分支还有内容没提交,切换分支会失败,需要commit或用git stash。如果不想要这些内容了,可以用git checkout --force。
1
git checkout chapter-two
Pushing a local branch remotely
Push the changes from the local chapter-two branch to create the remote branch chapter-two on GitHub.
1
git push --set-upstream origin chapter-two
Merging an existing branch into the current branch: git merge
Make a commit on the local branch named chapter-two and merge this into the master branch.
git pull约等于git fecth && git merge.
Deleting the current local branch after merging
Delete the local branch named chapter-two.
1 2 3
cd /Users/mike/GitInPracticeRedux/ git checkout master git branch --delete chapter-two
Part 2 Git essentials
Filesystem interactions
Renaming or moving a file: git mv
Rename a previously committed file named GitInPractice.asciidoc to 01-IntroducingGitInPractice.asciidoc and commit the newly renamed file.
1 2 3
cd /Users/mike/GitInPracticeRedux/ git mv GitInPractice.asciidoc 01-IntroducingGitInPractice.asciidoc git commit --message 'Rename book file to first part file.'
Removing a file: git rm
Remove a previously committed file named GitInPracticeReviews.tmp in your Git working directory and commit the removed file.
# reset all files to their last-committed state and remove all uncommitted files. git reset --hard git clean -xdf
Temporarily stashing some changes: git stash
Stash all your uncommitted changes for later retrieval.
Create a temporary commit with a prepopulated commit message and then returns your current branch to the state before the temporary commit was made.
1 2 3 4
cd /Users/mike/GitInPracticeRedux/ echo EXTRA >> 01-IntroducingGitInPractice.asciidoc git stash save git stash list # 可以看所有stash
Reapplying stashed changes: git stash pop
Pop the changes from the last git stash save into the current working directory.
1 2 3
cd /Users/mike/GitInPracticeRedux/ git stash pop # 会弹出最顶上的stash git stash apply # 不会弹出最顶上的stash,只是应用,于是可以应用多次
Finding which commit caused a particular bug: git bisect
Locate the commit that renamed GitInPractice.asciidoc to 01-IntroducingGitInPractice.asciidoc
在使用之前要commit或stash当前内容,因为bisect操作将当前目录checkout到该相关commit。
1 2 3 4 5
cd /Users/mike/GitInPracticeRedux/ git bisect start git bisect bad git bisect good 6576b6 git bisect reset
Merging branches and always creating a merge commit
Merge the chapter-spacing branch into the master branch and create a merge commit, not perform a fast-forward merge.
1 2 3
cd /Users/mike/GitInPracticeRedux/ git checkout master git merge --no-ff chapter-spacing
Resolving a merge conflict
Merge the separate-files branch into the master branch and resolve the resulting merge conflict.
Resolving each merge conflict only once: git rerere
rerere代表Reuse Recorded Resolution。
Set up git rerere to integrate with the merge workflow so you don’t need to repeatedly resolve the same merges.
Creating a tag: git tag
A tag is another ref(or pointer) for a single commit. Tags differ from branches in that they’re (usually) permanent.
Tag the current state of the GitInPracticeRedux master branch as version v0.1。
1 2 3 4 5
cd /Users/mike/GitInPracticeRedux/ git checkout master git tag v0.1 git tag # 显示当前repository的所有tag git push -tags
可用–delete删除某tag。
Generating a version number based on previous tags: git describe
Generate a version number for a software project based on existing tags in the repository.
1 2
cd /Users/mike/GitInPracticeRedux/ git describe --tags
Adding a single commit to the current branch: git cherry-pick
cherry-pick a commit from the v0.1-release branch to the master branch.
Rebasing commits on top of another branch: git rebase
Rebase the inspiration branch on top of the v0.1-release branch.
1 2
git checkout inspiration git rebase v0.1-release
Rebasing commits interactively: git rebase --interactive
Interactively rebase the history of a branch. 应该常用。
1 2 3 4 5 6
git checkout inspiration git rebase --interactive v0.1 # 会弹出一个editor 输入 pick 6d4ad83 Add Chapter 1 inspiration. p df32377 Advanced practice technique. f a8200e1 Add release preface.
Pulling a branch and rebasing commits: git pull --rebase
避免出现merge commit,常用。
Pull commits from origin/master and rebase your current commits in master on top of the upstream changes.
1
git pull --rebase # 等价于git fetch && git rebase
Rewriting history on a remote branch: git push --force
Rewrite the history on the remote origin/inspiration branch based on the contents of the local inspiration branch.
Learn how to maintain dependencies on other Git-based software projects within your own using Git submodules.
把别人用git管理的开源代码库作为自己代码的submodule。
Git’s submodules store the reference to a specific SHA-1 reference in another remote Git repository and store these in subdirectories of the current repository.
Adding a git submodule: git submodule add
Add a the GitInPracticeReduxSubmodule repository as a submodule of the GitInPracticeRedux repository in the master branch.
Showing the status of submodules: git submodule status
Show the current states of all submodules of a repository.
1 2
cd /Users/mike/GitInPracticeRedux/ git submodule status
Updating and initializing all submodules: git submodule update --init
Initialize all submodules in your repository and update them to the latest revision.
1 2
cd /Users/mike/GitInPracticeRedux/ git submodule update --init
Running a command in every submodule: git submodule foreach
Output some status information for every submodule in the GitInPracticeRedux repository.