A. Getting Started with GitI started using git six months ago, and I've been working with SVN and CVS. Git commands are many and seem complicated, but it's enough to learn a few simple things at the very beginning. I believe that many people have used the SVN before using git, which does not discuss the differences between Git and SVN, and does not compare and comment on these two code management software, just some records in my personal use. SVN needs to build an SVN server (HTTP/SVN), to build the SVN server need to read some manuals git does not need a git server, any git-installed PC can be a git server, other people can use any existing way (ssh/http/ Local file system) update the upload code I have two pc:a and B, they are in a network segment (192.168.6.0/24), in order to make this git use record more rigorous, each of the following commands are tested on both PCs. Where a address is 192.168.6.1, b address is 192.168.6.15, B can be logged in via SSH to a
1. Initialize a git directory on a machine recently started to learn Scala, on a machine has a directory is/home/le/workspace/scala_test, in this directory under the initial session of a git directory
$ git init
At this point you will find a. git directory in the current directory $ ls-la
2. Submit files to the Git repository
$ touch Test1.scala $ vim Test1.scala
$ git status. #git会告诉你新的改动, next select the file you want to submit
$ git add test1.scala
$ git status. #再来看看, Git will tell you what you're going to commit
$ git commit-m ' initial git repo ' #提交到本地branch
A can be used as a git server and next to B.
2. Clone code from A to B, located in/home/le/workspace/git/repo1/
$ git clone le@192.168.6.1:/home/le/workspace/scala_test
3. Create a branch, switch branches, commit branches to a remote git library
$ git Branch scala_dev#创建分支
$ git checkout scala_dev#切换到该分支
$ git Branch#查看本地当前的分支
$ Git push origin Scala_dev#提交scala_dev分支到远程git库
$ git branch-r#查看远程所有的分支
$ git branch-a#查看本地和远程所有的分支
4. Modify the file and submit it to the remote library
$ Touch Test2.scala
$ git Add Test2.scala
$ git commit
$ git log #在push之前确认commit是否正确
$ git push
5. Clone a specified branch from a remote git library, this time cloning a copy of the code in the/home/le/workspace/git/repo2/directory of the B machine
$ git clone-b scala_dev le@192.168.6.1:/home/le/workspace/scala_test
$ git Branch #查看当前分支
#提交新的代码
$ Touch Test3.scala
$ git Add Test3.scala
$ git commit
$ git log #在push之前确认commit是否正确
$ git push
6. Back to/home/le/workspace/git/repo2/Update code
$ git pull #如果有错误, follow the error prompts to modify the. git/config file
B. Some issues that may be encountered 1. Merging multiple commits one commit has been made several times in the local Git repository, which can be merged locally when pushing to a remote git repository. Merge two commits at a time
$ git reset--soft head^1
$ git commit--amend merge last n commit
$ git rebase-i head~n
#替换第n个pick为s保存
2. Cancel the local commit
Sometimes commits the code to the local git library, for some reason to delete the last 1 commits, you can directly revert to the previous commit
$git Reset--hard head~1
3. Using Git stash
If you use Gerrit as me to do code review, you might encounter a problem where multiple bugs on the same branch need to fix,fix a bug and then wait for code review on the commit code to Gerrit server. In the process of waiting to start fix another bug, just before this time the code submitted by reviewer prompted an error, need to update the previous code, this time can use Git stash to save the current local changes. Modify the code on a branch but do not want to commit to the local git library, you need to switch a branch to modify the code, this time can also use Git stash
$ git stash
# Modify your code or checkout to another branch
# git commit--amend
$ git stash pop
4. Pick some commits from the other branch to the current branch
If you have two branches, one is the dev branch, one is the release branch, there is a lot of commit on the dev branch, and sometimes you need to pick some commits from the Dev branch to the release branch.
$ git log #查找到需要的commit ID, assumed to be 5b531cb8c62cdb8b5f10c406e41824a6388e3a82
$ git Checkout release
$ git log #看一下当前的commit log
$ git cherry-pick 5b531cb8c62cdb8b5f10c406e41824a6388e3a82
$ git log #此时可以看到commit log added
5. appear error:the following untracked working tree files would be overwritten by checkout
$ git clean-d-fx ""
6. I commit 5 files at a time to the Gerrit above, and then found that there is no need to submit, just submit 4, I would like to resubmit the same commit ID below, what I should do.
The prerequisite code has not been merge into the library
For example, to cancel file 2.txt, then restore 2.txt to commit ID 9a42e8e3f41dcf5029a11ce651f36cbdf37fc84d before you commit the commit ID
git reset 9a42e8e3f41dcf5029a11ce651f36cbdf37fc84d--2.txt
Git checkout--2.txt
git Add.
git commit--amend
C. Some reference materials
http://www.vogella.com/articles/Git/article.html [Chinese Translation version]
https://www.kernel.org/pub/software/scm/git/docs/
http://pcottle.github.com/learnGitBranching/[This is fun]