git simple tutorial

Source: Internet
Author: User
Tags commit gpg ini tagname ssh git clone git commands
I first published and summed up the things I learned, there are bad places please point out one, git upload item (install git)

There are two ways git uploads a project: An HTTPS method, an SSH method

HTTPS method:

1, first login github.com registered account to create a newrespository

2, install git, tutorial self-Baidu Search

3. On the local project, right-click Git base here to enter the GIT init project on the command line to add git management

4. Enter git Add. (Don't forget ".") ) to add all the file contents of the project to Git

5. Enter Git commit–m "First commit (Prompt information input)"

6. Enter git remote add Origin https://github.com/youname/xiangmuming.git, link to your github repository

7. Enter Git push–u origin master, upload your project to GitHub and ask for your GitHub account and password

Submit project to this first upload successfully

Attention:

In addition, there is a pit to note, is in the seventh step above the creation of the remote repository, if you check the Initializethis repository with a readme (that is, the creation of the warehouse automatically when you create a readme file), So in the Nineth step, when you push the local repository content to the remote repository, you will report a failed to push Somerefs to https://github.com/guyibang/TEST2.git error.

This is because the Readme file in your newly created warehouse is not in the local repository directory, so we can merge the following commands first:

Git pull--rebase Origin master

Then you can push and you will succeed.

Common git commands

Git Status View the status of the workspace

Git diff to see what has been changed

second, time shuttle machine One, version fallback

Enter git log (or git log–pretty=oneline) to view the commit history

Enter git reset--hard head^ (^ fallback previous version, ^^ fallback previous version, 100 input head~100), or enter git reset--hard commit_id (Commit history ID)

Enter Git reflog to view the command history to determine which version to return to in the future

Ii. working area and staging Area (stage)

After file modification, add git add + filename to Staging area (stage), then commit git commit–m "Submit Comment"


third, management changes

Git tracks and manages changes, not files.

Understand how git tracks changes: Every time you change, if you don't git add to Staging Area (stage), you won't be added to commit Iv. undo Changes

1, the command git checkout--readme.txt means, the Readme.txt file in the workspace changes are all revoked, here are two cases:

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.

2. If you have added to staging area, you can use git reste HEAD file (filename) to undo staging area changes.

The git reset command either rolls back the version or rolls back the staging area changes to the workspace. When we use head, we represent the latest version

3, if you have submitted, you can make version fallback operation v. Delete a file

To remove from the repository:

Git commit–m "comments", rm filename, git rm filename

Deleted the wrong: git checkout filename

third, remote warehouse

Generate SSH key:ssh-keygen–t rsa–c "Mailbox"; Login to GitHub set SSH key One, add remote repository

Request a GitHub account to create a project

Local git repositories associated with remote repositories

Git Remote Add Origin Git@github.com:yougithubaccount/xiangmu.git

Local project push to remote repository Git push–u Origin master (first push to add –u, later push no longer requires-u)

, push all the contents of the master Branch second, clone

Use git clone + address

GIT supports multiple protocols, including HTTPS, but the native GIT protocol supported by SSH is the fastest. Iv. Branch Management First, create and merge branches

1. Create and switch branches: git checkout–b dev

The Git Checkout command plus the-b parameter means create and switch, equivalent to two commands

Git branch dev (create), git checkout dev (switch branch)

2. View the current branch of GIT branch (list all branches, Zhiqian *)

3, the operation of the work area normal operation

4. Merging branches:

First switch to Master branch master,

Execute git merge dev to specify branch merge to current branch

5. Delete branch: git branch–d dev Delete after view branching git branch merge conflict solution:

6. Merge conflicts, resolve conflicts manually in main branch, then resubmit, merge complete

git log--greph View branch merge diagram (or git log--graph--pretty=oneline--abbrev-commit) Two, branch merge strategy

Typically, the merge branch, if possible, Git uses fast forward mode, but in this mode, the branch information is discarded after the branch is deleted.

If you 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.

git merge with--no-ff mode (merge branch)

Merge input (disable Fast forward mode): git merge--no-ff–m "comment" Dev

(Because this merge is going to produce a new commit, add the-m parameter and write the commit description in)

third, the bug branch

When fixing a bug, we will fix it by creating a new bug branch, then merging and finally deleting;

When the work is not finished, first put the work site Git stash, and then to fix the bug, repair, then git stash pop, back to the job site.

Store current working state: Git Stash

View a list of stored work states: Git stash list

Retrieve work Status:

1, git stash apply (remove the latest storage work status after recovery, stash will not be deleted), Git stash drop (delete)

2, Git stash pop (resume working status, delete stash information)

Restore the specified working state: Git stash apply stash@{0} forcibly Delete branch:

Create a branch, add a new feature, after the development is completed, to the task does not need the feature, and then we delete the branch git branch–d Dev, which is prompted to delete the failure, because the Dev branch is not merged, need to force Delete, input command: git branch–d dev ("- D "to"-D "is forced to delete)

v. Branch Management One, view remote library information

1. View Remote Library information: Git remote

2. View more information: Git remote–v two, push branch

1. Push branch: Git push Origin master (main branch, other branch git push Origin dev) three, crawl branch

1. Create a remote branch: Git checkout–bdev origin/dev

2. The local branch is associated with the remote branch: Git branch--set-upstream Dev origin/dev Five, tag management

When we publish a version, we usually start with a tag (tag) in the repository, so that it is the only version that determines the time of the tag. Any time in the future, the version of a tag is taken out of the historical version of that tag. Therefore, the tag is also a snapshot of the repository.

Git's tag is a snapshot of the repository, but it's actually a pointer to a commit (much like a branch is right.) However, the branch can be moved and the label cannot be moved, so creating and deleting tags is instantaneous.

Git has a commit, why introduce tag.

"Please release the last version of Monday, the commit number is 6a5819e ..."

"A bunch of messy numbers are not easy to find. ”

If a different approach:

"Please release the last version of Monday, the version number is v1.2"

"OK, just follow tag v1.2 to find commit." ”

So, tag is a meaningful name that's easy to remember, and it's tied to a commit. First, create a label

1. Create tag: git tag <name> (the default tag is on the latest commit)

2. See all Tags: git tag

3, if yesterday forgot to hit the label how to do.

First, find the commit_id of the history submission and label it.

Input: Git log–abbrev-commit (or git log--pretty=oneline--abbrev-commit)

Find the Commitid that need to be labeled

Input: git tag v0.9 commitid

4. View tag information: Git show tag

5. Create a label with description,-A To specify the table signature,-M to specify the description text:

Git tag–a v0.9–m "comment" Commitid

You can also sign a label with the private key through-s:

git tag-s v0.2-m "signed version 0.2 released" Commit ID: signed with PGP signature, therefore, must first install GPG (GnuPG), if not found GPG, or no GPG key pair, will be error

Second, delete the label

1. Delete local tags: git tag–d <tagname>

2. Push to specify local tags: Git push origin <tagname>

3. Push all Tags: git push origin--tags

4. Delete Remote Tags:

Delete local tags first: git tag–d <tagname>;

Then delete the remote tag: git push origin:refs/tags/<tagname>


Delete an existing associated remote repository: Git remoterm origin Vi. Association of multiple remote warehouses

1. Delete the associated remote repository:

Gitremote RM <name> (the name of the remote repository, named by itself)

2. Associate the first remote repository: Git remote add github git@github.com:youname/project.git

Because of the associated GitHub so named GitHub

3. Associate a second remote repository: git remote add gitee git@gitee.com:younmae/project.git

Because the associated code cloud so named Gitee

4. Push command is divided into two:

git push github master; git push giteemaster

vii. customizing Git I. Ignoring special documents

Create a special ". Gitignore" file under the root of the Git workspace, and then fill in the file name you want to ignore, and git will automatically ignore the files.

There is no need to write from scratch. gitignore file, GitHub has prepared a variety of configuration files for us, just a combination to use. All profiles can be viewed directly online: Https://github.com/github/gitignore

The principle of ignoring files is:

1, ignoring the operating system automatically generated files, such as thumbnail images;

2, ignoring the compilation of intermediate files, executables, etc., that is, if a file is automatically generated through another file, then the automatically generated files will not need to put into the repository, such as the Java compiler generated by the. class file;

3. Ignore your own profile with sensitive information, such as the configuration file that holds the password.

For example, Python development:

# Windows: Auto-generated files need to be ignored

Thumbs.db

Ehthumbs.db

Desktop.ini

# Python:python files generated at compile time need to be ignored

*.PY[COD]

*.so

*.egg

*.egg-info

Dist

Build

# My configurations: Write your own definition of some files that need to be ignored

Db.ini

Deploy_key_rsa

Last Commit git

Forced commit ignored files: git add–f filename

Problem adding file, check ignore file: git check-ignore-vfilename git command config alias

The--global parameter is a global parameter, which is useful for all git repositories on this computer

git config--global alias.st status----> Git status---->git St

git config--global alias.co checkout---->git Checkout---->GIT Co

git config--global alias.ci commit---->git commit---->GITCI

git config--global alias.br branch---->gitbranch---->git BR

git config--global alias.unstage ' resethead '---->git reset HEAD file---->git unstage file

git config–global alias.last ' log-1 '---->git log-1---->gitlast

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

alias. LG "Log--color--graph--pretty=format: '%cred%h%creset-%c (yellow)%d%creset%s%cgreen (%CR)%c (bold Blue) <%an>%creset '--abbrev-commit '

Look at the effects of git LG:

configuration file:

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

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

[Core]
repositoryformatversion = 0
True
False
True
True
True
[remote "origin"]
URL = git@github.com:michaelliao/learngit.git
Fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
Remote = origin
Merge = 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 = Checkout
CI = Commit
BR = Branch
St = Status
[User]
Name = YourName
email = your@email.com

Configuration aliases can also directly modify the file, if the wrong, you can delete the file re-configuration through the command. third, 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.

1. Installing Git:sudo apt-get Install git

2. Create a git user to run the git service: sudo adduser git

3. Create a login certificate: Collect all the public keys of the users who need to log in, their own id_rsa.pub files, import all the public keys into the/home/git/.ssh/authorized_keys file, one line at a.

4. Initialize git repository: Select a directory: sudo git init--bare Sample.git;git will create a bare repository, the bare repository does not have a workspace, because the GIT repository on the server is purely for sharing, so users are not allowed to log on to the server directly to the workspace, and the Git repositories on the server usually end in. git. Then, change owner to Git:sudo chown-r Git:git sample.git

5. Disable 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

Switch

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

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.

6. Clone remote repository: Git clone git@server:/srv/sample.git

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: info-contact@alibabacloud.com 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.