Git usage notes-transferred from Liao's predecessor's website

Source: Internet
Author: User
Tags tagname using git git commands

git installation
1.linux Installing Git
sudo apt-get install git older: sudo apt-get install Git-core

2.maxOS Installing Git
Brew Install Git Installing Git
Msysgit is a Windows version of Git, downloaded from, and then installed by default. After the installation is complete, start
menu, find Git Bash, and pop out a command-line window that shows the GIT installation is successful!
After the installation is complete, you will need the final step, and at the command line, enter:
$ git config--global "Your name"
$ git config--global "[Email protected]"

To create a version library
1. Create repository--Repository Add File ". Git" file

$ mkdir Learngit
$ CD Learngit
$ pwd
Turn the above directory into a repository
$ git init
Initialized Empty Git repository in/users/michael/learngit/.git/
Initialize a git repository using the git init command.

Add files to the Git repository in two steps:
The first step, use the command git add <file>, note, can be used repeatedly, add multiple files;
The second step is to use the command git commit to complete.
3. Viewing the current repository status--git status

If you have only modified the no git add
Git status tail line shows--no changes added to commit (use "git add" and/or "Git Commit-a")
If only git add has no git commit
Git status second line shows--changes to be committed
If the file you submitted for the first time is not displayed by git add
Changes not staged for commit:
(use "git add <file> ..." To update what would be committed)
(use "Git checkout--<file> ..." to discard changes in working directory)
untracked files:
(use "git add <file> ..." to include in what'll be committed)
The above code indicates that the Readme.txt file has been modified and license has never been added ******************

Version fallback
The 1.git log command displays the commit log from the most recent to the furthest, important parameter--pretty=oneline
2.git fallback version (back to the past)

In Git, the head represents the current version, the previous version is head^, the last version is head^^, of course, 100 to write 100 ^
It's easier to count, so write head~100.

$ git reset--hard head^
$ git reset--hard 3628164
Back to the previous version
3.git Back to future version (back to future)
git reflog your git commands every time.
Can use this back to the future version
4. Now summarize:
The version that head points to is the current version, so git allows us to travel between versions of history, git reset--hard commit_id.
Before the shuttle, use git log to view the commit history to determine which version to fallback to.
To return to the future, use Git reflog to view the command history to determine which version to go back to in the future.

Workspaces and Staging Area
1. Work area-local branch of the thing
2. Staging Area--git Add after we think that the code was submitted to the staging area

git admin changes
1. Discard changes to the workspace
Git checkout--readme.txt
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 status
# on Branch Master
# Changes to be committed:
# (use "git reset HEAD <file> ..." to Unstage)
# Modified:readme.txt
Git Reset HEAD readme.txt can undo staging area's changes (Unstage), and the workspace will change to the state before Git add
2.git Management Summary
Git checkout--readme.txt to discard workspace changes
git reset HEAD readme.txt Discard cache changes to the state before Git add

deleting files
1.$ RM test.txt
# changes not staged for commit:
# (use "Git add/rm <file> ..." To update what'll be committed)
# (use "Git checkout-<file> ..." to discard changes in working directory)
RM Direct Delete is to delete files in the workspace
A. If your goal is to remove the repository, you need git add and git commit
B. If you think it's a mistake, you can use git checkout-test.txt to get the file back from the repository.
2. Summary
Command git rm to delete a file. If a file has been submitted to the repository, then you will never have to worry about accidental deletion, but be careful that you can only restore
To the latest version, you will lose the content you modified after your last commit.

Set up a remote repository
1. Setting up a remote repository
A. Create an SSH Key.
In the user's home directory, see if there are any. SSH directories, and if so, see if there are any id_rsa and in this directory.
These two files, if you already have one, can jump to the next step directly. If not, open the shell (open git Bash under Windows).
Create SSH key:ssh-keygen-t rsa-c "[Email protected]"
B. Login to GitHub and open the "Account Settings", "SSH Keys" page:
Then, click "Add SSH Key", fill in any title, paste the contents of the file in the Key text box:
Another way is to do it yourself, take a git server, because it is your own git server, so others are invisible.
C. Set up a remote branch from GitHub to remember the GIT address
Touch README.MD//Create a new document for record submission operations
GIT init//Initialize local repository
git add
Git commit-m "First commit"//submit to the repository and write some notes
git remote add origin [email protected]om:youname/test.git//Connect remote repository and build a name: Origin
Git push-u origin master//submit something from the local repository to address is Origin's address, under Master Branch
2.github problem
/**linux Command Review **/
Delete all the name under the current folder containing the. svn file
Find. -name ". SVN" | Xargs RM-RF
Delete all the name under the current folder that contains the. git file
Find. -name ". Git" | Xargs RM-RF
/**linux Command Review **/

If you enter Git remote add origin [email protected]:d Jqiang (GitHub account name)/gitdemo (project name). Git
Tip error Message: Fatal:remote origin already exists.
The solution is as follows:

A, first enter the GIT remote RM origin
b, then enter the GIT remote add origin [email protected]:d Jqiang/gitdemo.git will not error!
C, if you enter git remote RM origin or an error,
Error:could not remove config section ' Remote.origin '. We need to modify the contents of the Gitconfig file
D. Find the installation path for your GitHub,
Mine is C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4a6720bb5c8\etc.
E, find a file named Gitconfig, open it to the [remote "origin"] that line to delete it!
If you enter $ ssh-t [email protected]
Error message: Permission denied (PublicKey). Because the newly generated key cannot join SSH, it will cause the connection to not be on GitHub.
The solution is as follows:

A, enter $ ssh-agent First, and then enter $ Ssh-add ~/.ssh/id_key, so you can.
b, if still not, enter Ssh-add ~/.ssh/id_key command
An error occurred could not open a connection to your authentication agent. The solution is key with Git Gui
SSH tool generated, so that when generated by the key is stored directly in SSH, no need to Ssh-add command added, the other User,token
Configuration is done using the command line.
c, it is better to check if you copy the contents of the file there is no extra space or blank lines, some editors will help you add these.
If you enter a GIT push origin master
Tip error message: error:failed to push som refs to ....
The solution is as follows:

A, first enter the GIT pull Origin master//first put the remote server github above the file down
B. Re-enter $ Git push origin master
C, if there is an error fatal:couldn ' t find remote ref Master or
Fatal: ' Origin ' does not appear to be a git repository and
Fatal:could not read from remote repository.
D, you need to re-enter the git remote add [email protected]:d Jqiang/gitdemo.git

Add a remote repository
To associate a remote library, use the command git remote add origin [email protected]:p ath/repo-name.git;
Once associated, use the command Git push-u Origin master to push all the contents of the master branch for the first time;
Thereafter, after each local commit, whenever necessary, you can use the command GIT push Origin master to push the latest changes;

Cloning from a remote repository
$ git clone [email protected]:michaelliao/gitskills.git
GIT supports multiple protocols, and the default git://uses SSH, but other protocols such as HTTPS can also be used.
With HTTPS, you must enter a password for each push except for slow speed, but you cannot use the SSH protocol in some companies that only open HTTP ports
and only with HTTPS.

Create and Merge Branches
GIT encourages the use of branching:
View branches: Git branch
Create a branch: Git branch <name>
Switch branches: git checkout <name>
Create + switch branches: git checkout-b <name>
Merge a branch to the current branch: git merge <name>
Delete branch: Git branch-d <name>

Resolve Conflicts
$ git log--graph--pretty=oneline--abbrev-commit Branch Merge diagram

Branch management policy?
Master Branch Master The cleanest branch does no action on this branch
Develop Branch Dev Developer's main work branch, used for cycle development, rapid iterative development
Modify a bug Fixbug a branch of a bug

??????????????????? A very important chapter--Start???????????????????
Fixbug Branch
Application Scenarios
You need to modify a master (online) branch of a very urgent bug, the bug needs to be resolved within 1 hours, the Dev branch function development to half Also
It takes a long time (much more than 1 hours), what do you need to do at this time?
You can do this:

Git checkout-b fixbug
Then modify the bug (now the bug has been modified on the latest Fixbug branch checked out from master)---modification completed
git Add./
git commit-m ' modify bug '

git checkout Dev
$ git stash
# Saved working directory and index state WIP on dev:6224937 add merge
# HEAD is now at 6224937 add merge

git status
# from
# * Branch Dev-and Fetch_head
# already up-to-date.

git merge fixbug--no-ff-m ' Modify the bug, merge the contents of the Fixbug branch to Dev '
The work site is still there, git put stash content somewhere, but need to restore, there are two ways:
One is to use git stash apply recovery, but after recovery, stash content is not deleted, you need to use git stash drop to delete;
Another way is to use git stash pop, restore the stash content also deleted
You can stash multiple times, restore the time, first use Git stash list to view, and then restore the specified stash, with the command:
$ git stash apply [email protected]{0}

Git stash pop (you can then use Git stash list to see what's going On)

git checkout Master
git merge fixbug--no-ff-m ' Modify the bug, merge the contents of the Fixbug branch to Dev '

Then delete fixbug branch git branch-d fixbug (-D destroy Fixbug branch-D forced Destroy)

This is done in case there is no conflict when the Dev branch is completed and the master branch is merged

??????????????????? A very important chapter--The end???????????????????

Multi-person collaboration
1. When you clone from a remote branch using Git clone, the local master branch and the remote Master branch are automatically mapped, and the remote repository default
Name defaults to Origin

View Remote Library branch git remote or more information about Git remote-v
Origin [email protected]:michaelliao/learngit.git (FETCH)
Origin [email protected]:michaelliao/learngit.git (push)

Push local branch to remote branch
Git push Origin branch name (the local branch name is generally the same as the remote branch name)

# Specify which branch you want to the merge with.
# See Git-pull (1) for details

# git pull <remote> <branch>
# If you wish to set tracking information for this branch you can do so with:
# git branch--set-upstream Dev origin/<branch>

Above is the steps required for Git pull failure

To set up a link for Dev and origin/dev:
$ git branch--set-upstream Dev origin/dev
2. Multi-person collaboration steps
As a result, the working mode of multi-person collaboration is usually this:

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

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

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

D. With no conflict or resolve conflict, then GIT push origin branch-name push will be successful!

E. If git pull hints "No tracking information", then the link relationship between the local branch and the remote branch is not created,
Use 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.

Label Management
1.git Dozen Tags
Switch to the branch that needs to be labeled
git tag v1.0 or
Git tag-a <tagname>-M "Blablabla ..." Specify labels for label notes
View all Tags
git tag
View label Information
Git Show v1.0
2.git label operation
Delete a label
$ git tag-d v1.0
Push local tags to remote
Git push Origin <tagname>
For example GIT push Origin v1.0
Push locally all tags that failed to push to remote
$ GIT push origin--tags

If the label has been pushed to the remote, to remove the remote label is a bit troublesome, first removed from the local:
$ git tag-d v0.9
# Deleted Tag ' v0.9 ' (was 6224937)

$ git push origin:refs/tags/v0.9
# to [email protected]:michaelliao/learngit.git
#-[deleted] v0.9

customizing git
1. Having git display colors will make the command output look more visible:
$ git config--global color.ui true
2. Configure shortcut keys
$ git config--global status (git status = Git st)
$ git config--global checkout (git checkout = Git co)
$ git config--global commit (git commit = Git ci)
$ git config--global branch (git branch = git br)
How to delete a git configuration file shortcut key
Just go to the user's home directory to find the. gitconfig file and delete the corresponding row. CD ~/

git config--global--unset

View Configuration aliases
git config--list | grep alias
git config--get-regexp alias

Build a git server
Building a git server requires a machine running Linux, which is highly recommended for Ubuntu or Debian, so that with a few simple apt commands you can
To complete the installation.

The first step is to install the GIT service
$ 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:
/home/git/.ssh/authorized_keys file, one line at a.
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 users are not allowed to log in directly to the service
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:
git:x:1001:1001:,,,:/home/git:/bin/bash instead
This way, git users can use git normally via SSH, but can't log in to the shell because the Git-shell we specify for git users is
Automatically exits.
Sixth, 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.git

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.

Git usage notes-transferred from Liao's predecessor's website

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.