Git Learning Notes Collation

Source: Internet
Author: User
Tags aliases version control system

git learn notes 1. Install Git
注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置   $ git config --global "Your Name"$ git config --global "[email protected]"
2. Create a version library-repository
$ mkdir learngit$ cd learngit$ pwd/Users/michael/learngit//pwd命令用于显示当前目录。在我的Mac上,这个仓库位于/Users/michael/learngit。
3. Initialize git (turn this directory into a git managed directory)
$ git init
4. Add files to Repository
$ git add readme.txt$ git add file2.txt file3.txt$ git commit -m "备注有意义的信息"
5. View Warehouse Status
$ git status
6. Version fallback
$ git log 或者 $ git log --pretty=oneline //查看提交日志 首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。$ git reset --hard HEAD^ //返回到上一个版本(穿越回去)$ git reset --hard 3628164 //回到现在之后的某个版本(穿越回来)
7. Display the command log for each commit
$ git reflog
8. Workspaces and Staging Area

The first step is to use git add to add the file, in fact, the file is modified to add to the staging area;

The second step is to commit the change with git commit , which is actually to bring all the contents of staging area > into the current branch.

9. Management changes
用git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别$ vi xxx.txt 编辑文件 wq保存
10. Undo Changes
$ git checkout -- readme.txt//工作区$ git reset HEAD readme.txt//暂存区

The command git checkout–readme.txt means to undo all changes to the Readme.txt file in the workspace, here are two things:

One is that Readme.txt has not been put into the staging area since the modification, and now, the undo changes back to the same state as the repository;

One is that the Readme.txt has been added to the staging area and modified, and now the undo changes go back to the state that was added to the staging area.

All in all, let this file go back to the state of the last git commit or git Add.

Git checkout–file command – very important, no – it becomes a "switch to another branch" command

11. deleting files
$ rm test.txt //文件管理器或者命令删除工作区文件//下面是删除版本库里的$ git rm test.txt$ git commit -m "remove test.txt"
12. Remote Storage

1th step: Create SSH Key. In the user home directory, see if there is no. ssh directory, if there is, then look at this directory there are no Id_rsa and these two files, if already have, can jump directly to the next step. If not, open the shell (open git Bash under Windows) and create SSH Key:

$ ssh-keygen -t rsa -C "[email protected]"

You need to change the email address to your own email address, and then return to the default value, as this key is not used for military purposes, so there is no need to set a password.

If all goes well, you can find the. SSH directory in the user's home directory, there are Id_rsa and two files, these two are SSH key key pair, Id_rsa is the private key, can not be leaked out, is the public key, can be assured to tell anyone.

2nd step: Login to GitHub, open "Account Settings", "SSH Keys" page ( this is a private warehouse ):

Then, click "Add SSH Key", fill in any title, paste the contents of the file in the Key text box:

Click "Add Key" and you should see the Key already added:

13. Add a remote Library

First, log on to GitHub, and then, in the upper right corner, find the "Create a new Repo" button to create a new warehouse.

In Repository name to fill in xxxx, other to keep the default settings, click the "Create Repository" button, you have successfully created a new Git repository

At this time, the XXXX warehouse on GitHub is still empty, and GitHub tells us that we can clone a new repository from this repository, or associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository.

Now, we're running the command under the local Learngit repository, based on GitHub's tips.

$ git remote add origin [email protected]:xh2015/testgit.git

Once added, the name of the remote Library is origin, which is the default term for git, or it can be changed to something else, but the name of origin is known as the remote repository.

Next, you can push all the contents of the local library to the remote library.

$ git push -u origin master

Pushing the contents of the local library to the remote, using the git push command, is actually pushing the current branch master to the remote.

Since the remote library is empty, when we first push the master branch, with the-u parameter, git will not only push the local master branch content to the remote new Master branch, but also associate the local master branch with the remote Master branch. You can simplify the command at a later push or pull.

After the push is successful, you can see in the GitHub page that the contents of the remote library are identical to the local

From now on, as long as the local commits, you can pass the command:

$ git push origin master

By pushing the latest changes to the local master branch to GitHub, you now have a truly distributed repository!

14. Cloning from a remote library
$ git clone
15. Create and Merge Branches
$ git checkout -b dev //相当于$ git branch dev$ git checkout dev ======================$ git branch //查看分支$ git checkout master //切回master分支 $ git merge dev//合并$ git branch -d dev //删除分支

16. Conflict resolution

Life is not as good as ten, and merging branches is often not smooth sailing.

After modifying the conflicting section, add commit

$ git log --graph --pretty=oneline --abbrev-commit //查看分支的合并情况$ git log --graph //可以看到分支合并图
17. Branch Management Policy

Typically, when merging branches, GIT uses fast forward mode if possible, but in this mode, the branch information is discarded when the branch is deleted.

If you want to force the fast forward mode to be disabled, GIT will generate a new commit at merge, so you can see the branching information from the branch history.

To prepare to merge the Dev branch, note the –NO-FF parameter, which means that Fast forward is disabled:

$ git merge --no-ff -m "merge with no-ff" devMerge made by the ‘recursive‘ strategy.readme.txt |    1 +1 file changed, 1 insertion(+)

Because this merge is going to create a new commit, add the-m parameter and write the commit description in.

Branching policy

In the actual development, we should follow several basic principles of branch Management:

First of all, the master branch should be very stable, that is, only to release the new version, usually do not work on it;

So where do you work? Work on the Dev branch, that is, the dev branch is unstable, at some point, such as the 1.0 release, then merge the Dev branch to master and release the 1.0 version in the Master branch;

You and your little friends each work on the Dev branch, and everyone has their own branch, and it's time to merge on the Dev branch.

So, the branch of teamwork looks like this:

18.Bug Branch
$ git stash //可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作

The work area is clean, where did you save the work site just now? Use the Git stash List command to see:

$ git stash list[email protected]{0}: WIP on dev: 6224937 add merge

The work site is still there, git put stash content somewhere, but need to restore, there are two ways:

$ git stash apply //恢复后,stash内容并不删除,你需要用$ git stash drop //来删除;$ git stash pop //恢复的同时把stash内容也删了$ git stash apply [email protected]{0} //恢复指定的一个
19.Feature Branch

In software development, there is always an endless amount of new features to add in.

$ git branch -D feature-vulcan (git branch -D <name>)//强制删除分支
20. Multi-person collaboration
$ git remote //查看远程库的信息$ git remote -v //显示更详细的信息

Push Branch

The push branch is the push of all local commits on that branch to the remote library. When pushing, specify the local branch so that git pushes the branch to the remote branch of the remote library:

$ git push origin master

If you want to push other branches, such as Dev, change to:

$ git push origin dev

However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which do not?

    • The Master branch is the main branch, so synchronize with the remote at all times;

    • The Dev Branch is a development branch, and all members of the team need to work on it, so they need to be synchronized with the remote.

    • Bug branches are only used to fix bugs locally, there is no need to push remote, unless the boss wants to see how many bugs you have fixed every week;

    • Whether or not the feature branch is pushed to the remote depends on whether you are working with your little partner to develop it.

In short, is in git, branch can completely in the local hide to play, whether to push, depending on your mood!

Multi-person collaboration

reads: 178355
When you clone from a remote repository, actually git automatically corresponds to the local master branch and the remote Master Branch, and the default name of the remote Repository is origin.

To view information for a remote library, use git remote:

$ git remote
Or, use git remote-v to display more detailed information:

$ git remote-v
Origin [email protected]:michaelliao/learngit.git (FETCH)
Origin [email protected]:michaelliao/learngit.git (push)
The address of origin that can be crawled and pushed is shown above. If you do not have push permissions, you cannot see the address of the push.

Push Branch

The push branch is the push of all local commits on that branch to the remote library. When pushing, specify the local branch so that git pushes the branch to the remote branch of the remote library:

$ GIT push origin master
If you want to push other branches, such as Dev, change to:

$ git push origin dev
However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which do not?

The Master branch is the main branch, so synchronize with the remote at all times;

The Dev Branch is a development branch, and all members of the team need to work on it, so they need to be synchronized with the remote.

Bug branches are only used to fix bugs locally, there is no need to push remote, unless the boss wants to see how many bugs you have fixed every week;

Whether or not the feature branch is pushed to the remote depends on whether you are working with your little partner to develop it.

In short, is in git, branch can completely in the local hide to play, whether to push, depending on your mood!

Crawl Branch

When working with multiple people, everyone pushes their own changes to the master and dev branches.

Now, simulate a small partner that you can clone on another computer (note to add SSH key to GitHub) or another directory on the same computer:

$ git clone [email protected]:michaelliao/learngit.gitCloning into ‘learngit‘...remote: Counting objects: 46, done.remote: Compressing objects: 100% (26/26), done.remote: Total 46 (delta 16), reused 45 (delta 15)Receiving objects: 100% (46/46), 15.69 KiB | 6 KiB/s, done.Resolving deltas: 100% (16/16), done.

When your partner clones from the remote repository, by default, your small partner will only see the local master branch. Do not believe you can use the GIT branch command to see:

$ git branch* master

Now that your little partner is developing on the dev branch, you must create a remote origin's dev branch locally, and he uses this command to create a local dev branch:

$ git checkout -b dev origin/dev

Now he can continue to modify it on Dev, then push the dev branch to the remote from time to moment:

$ git commit -m "add /usr/bin/env"[dev 291bea8] add /usr/bin/env1 file changed, 1 insertion(+)$ git push origin devCounting objects: 5, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 349 bytes, done.Total 3 (delta 0), reused 0 (delta 0)To [email protected]:michaelliao/learngit.gitfc38031..291bea8  dev -> dev$ git branch --set-upstream dev origin/dev //设置dev和origin/dev的链接

As a result, the working mode of multi-person collaboration is usually this:

1. First, you can try to push your own changes with GIT push Origin branch-name;

2. If the push fails, because the remote branch is newer than your local, you need to first try to merge with Git pull;

3. If there is a conflict in the merger, resolve the conflict and submit it locally;

4. No conflict or resolve the conflict, then GIT push origin branch-name push will be successful!

5. If git pull prompts "No tracking information", then the link relationship between the local branch and the remote branch is not created, using the command git branch–set-upstream branch-name origin/ Branch-name.

This is a multi-person collaborative mode of work, once familiar with, it is very simple.

21. Create a Label
$ git tag v1.0 //git tag <name>就可以打一个新标签$ git tag //可以用命令git tag查看所有标签

The default tag is on the most recently committed commit. Sometimes, if you forget to tag, for example, now is Friday, but should be in Monday hit the label did not hit, how to do?

The method is to find the commit ID submitted by the history and then hit it:

$ git log --pretty=oneline --abbrev-commit6a5819e merged bug fix 101cc17032 fix bug 1017825a50 merge with no-ff6224937 add merge59bc1cb conflict fixed400b400 & simple75a857c AND simplefec145a branch testd17efd8 remove test.txt...

For example, to tag the add merge commit, the commit ID is 6224937 and the command is typed:

$ git tag v0.9 6224937

Then use the command git tag to view the tags:

$ git tagv0.9v1.0

Note that labels are not listed in chronological order, but are sorted alphabetically. You can use git show to view tag information

$ git show v0.9$ git tag -a v0.1 -m "version 0.1 released" 3628164 //用-a指定标签名,-m指定说明文字$ git tag -s v0.2 -m "signed version 0.2 released" fec145a //通过-s用私钥签名一个标签
22. Operation Label
$ git tag -d v0.1 //删除标签

The created labels are stored locally only and are not automatically pushed to the remote. So, the wrong tag can be safely deleted locally.

$ git push origin v1.0 //要推送某个标签到远程,使用命令git push origin <tagname>$ git push origin --tags //一次性推送全部尚未推送到远程的本地标签

The label has been pushed to the remote, to remove the remote label just a little trouble, first remove from local

1.$ git tag -d v0.92.$ git push origin :refs/tags/v0.9
23. Customizing Git
$ git config --global color.ui true //让Git显示颜色,会让命令输出看起来更醒目
24. Ignoring special files

The principle of ignoring files is:

    • Ignoring the auto-generated files of the operating system, such as thumbnail images;
    • Ignoring compile-generated intermediate files, executables, and so on, that is, if a file is automatically generated from another file, the automatically generated file does not need to be put into the repository, such as Java compilation generated by the. class file;
    • Ignore your own configuration file with sensitive information, such as the configuration file that holds the password.

Create a. gitignore file in the workspace, adding content that needs to be ignored, such as:

# Windows:Thumbs.dbehthumbs.dbDesktop.ini# Python:*.py[cod]*.so*.egg*.egg-infodistbuild# My configurations:db.inideploy_key_rsa

The last step is to commit the. Gitignore to Git, and it's done! Of course, the standard for Gitignore is that the git status command does not say working directory clean.

Child shoes with Windows Note that if you create a new. gitignore file in the Explorer, it will be very mentally retarded to prompt you to enter the file name, but in the text editor "save" or "Save as" can save the files as. Gitignore.

There are times when you want to add a file to Git, but find that it is not added because this file was ignored by. Gitignore:

$ git add App.classThe following paths are ignored by one of your .gitignore files:App.classUse -f if you really want to add them.

If you do want to add the file, you can force it to git with-f:

$ git add -f App.class

Or you find that maybe. Gitignore write a problem, need to find out which rule is wrong, you can use the git check-ignore command check:

$ git check-ignore -v App.class.gitignore:3:*.class    App.class

Git will tell us that. Gitignore's 3rd line of rules ignores the file, so we know which rule to revise.

25. Configure aliases
$ git config --global status //告诉Git,以后st就表示status

Of course, there are other orders can be abbreviated, many people use Co to express Checkout,ci COMMIT,BR represents branch

$ git config --global checkout$ git config --global commit$ git config --global branch//提交就可以简写成:$ git ci -m "bala bala bala..."$ git config --global alias.unstage ‘reset HEAD‘//当你 $ git unstage 时候等于 $ git reset HEAD$ git config --global alias.last ‘log -1‘ → $ git last == $ git log -1 //查看最后一次提交信息

There are even people who have been desperate to configure LG as:

$ git config --global alias.lg "log --color --graph --pretty=format:‘%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset‘ --abbrev-commit" $ git lg 效果:

When configuring Git, plus –global is working for the current user, if not, it only works for the current warehouse.

Where's the configuration file? The GIT configuration files for each repository are placed in the. git/config file:

$ cat .git/config [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = [email protected]:michaelliao/learngit.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master[alias]last = log -1

Alias is immediately after [alias], to delete the alias, the corresponding line can be deleted.

The current user's git configuration file is placed in a hidden file in the user's home directory. Gitconfig:

$ cat .gitconfig[alias]co = checkoutci = commitbr = branchst = status[user]name = Your Nameemail = [email protected]

Configuration aliases can also directly modify the file, if the wrong, you can delete the file re-configuration through the command.

$ git config --list | grep alias 或者 git config --get-regexp alias//查看别名$ git config --global --unset //取消别名
26. Build a GIT server
    • Building a git server requires a machine running Linux and is highly recommended for Ubuntu or Debian, so you can install it with a few simple apt commands.

Assuming you already have sudo permissions for the user account, below, formally start the installation.

The first step is to install Git:

$ sudo apt-get install git

The second step is to create a git user to run the GIT service:

$ sudo adduser git

The third step is to create a certificate login:

Collect all the public keys of the users who need to log in, their own files, and import all the public keys into the/home/git/.ssh/authorized_keys file, one at a-line.

The fourth step is to initialize the GIT repository:

First select a directory as the Git repository, assuming/srv/sample.git, enter the command in the/SRV directory:

$ sudo git init --bare sample.git

Git creates a bare repository with no workspaces, because the GIT repository on the server is purely for sharing, so you don't let users log on to the server directly to the workspace, and the Git repositories on the server usually end up with. Git. Then, change owner to git:

$ sudo chown -R git:git sample.git

Fifth step, disable the shell login:

For security reasons, the GIT user created in the second step is not allowed to log in to the shell, which can be done by editing the/etc/passwd file. Find a line similar to the following:




In this way, git users can use git normally via SSH, but cannot log in to the shell because the Git-shell we specify for git users automatically exits every time a login is logged in.

Sixth step, clone the remote repository:

Now, you can clone the remote repository with the git clone command and run it on your own computer:

$ git clone [email protected]:/srv/sample.gitCloning into ‘sample‘...warning: You appear to have cloned an empty repository.

The rest of the push is simple.

Managing Public keys

If the team is small, it is possible to collect everyone's public key and put it in the server's/home/git/.ssh/authorized_keys file. If the team has hundreds of people, it is not possible to play this way, then you can use gitosis to manage the public key.

Here we do not introduce how to play gitosis, hundreds of people's team are basically in the 500 strong, I believe that a high level of Linux administrator problem is not big.

Manage permissions

There are many companies that not only depend on the source code such as life, but also depending on the employees as thieves, will be in the version control system set up a set of comprehensive permissions control, each person has read and write access to each branch or even under each directory. Because Git was developed for Linux source code hosting, Git also inherits the spirit of the open source community and does not support permission control. However, because git supports hooks, it is possible to write a series of scripts on the server side to control the commit and so on, to achieve the purpose of permission control. Gitolite is the tool.

Here we also do not introduce gitolite, do not waste the limited life in the power struggle.


Building a git server is easy, usually in 10 minutes;

To facilitate the management of public key, with Gitosis;

To be as perverted as SVN to control permissions, with Gitolite.


Access to Super Admins in Ubuntu

1. Enter in the terminal:

sudo passwd rootEnter new UNIX password: (在这输入你的密码)Retype new UNIX password: (确定你输入的密码)passwd: password updated successfully

Later, if you want to get root privileges, just do the following:

su rootPassword: (在此输入你上面设置的密码)

If you want to disable the root account again,
Then you can execute

sudo passwd -l root

2. On the command line, enter

After entering your user's password, will pop up a directory window, can be copied here, you can also delete the root file.
This is a graphical interface.

Git Learning Notes Collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.