GitHub is a git-based code hosting platform. Paying users can build private warehouses. Generally, free users can only use public warehouses, that is, code should be made public. For ordinary people, public warehouses are enough, and we do not have much code to manage, O (pipeline _ pipeline) O ~. Below are some simple usage methods I have summarized for beginners.
~~ Instructor Liao Xuefeng wrote a good tutorial on git. You can check it out on Baidu.
1. register an account and create a warehouse
To use GitHub, the first step is to register a GitHub account. Then you can create a warehouse (free users can only create a public warehouse), create a new repository, fill in the name, create, and some warehouse configuration information will appear, this is also a simple git tutorial.
2. Install the client msysgit
GitHub is a server. To use git on your computer, we still need a git client. Here I use msysgit, which only provides the core features of git and is based on command line. If you want a graphical interface, you only need to install tortoisegit on the basis of msysgit.
After installing msysgit, right-click and choose "Git init here" in the local repository to add a. Git folder, which indicates that the local git is successfully created. Right-click git bash and enter the GIT command line. to upload the local repository to GitHub, you also need to configure the SSH key.
3. Configure git
First, create an SSH key locally;
$ ssh-keygen -t rsa -C "[email protected]"
Change [email protected] to your email address, and then you will be asked to confirm the path and enter the password. We will use the default one-way carriage return. If it succeeds ~ /, Generate the. Ssh folder, go in, open id_rsa.pub, and copy the key.
Go back to GitHub, go to account settings, select SSH keys on the left, add SSH key, enter the title, and paste the key. In git bash, enter:
$ ssh -T [email protected].com
If this is the first time, you will be prompted whether to continue. If you enter yes, you will see: You 've successfully authenticated, but GitHub does not provide shell access. This indicates that GitHub has been successfully connected.
Next, we need to upload the local repository to GitHub. Before that, we need to set username and email, because every time GitHub commit records them.
$ git config --global user.name "your name"
$ git config --global user.email "[email protected]"
Go to the repository to be uploaded, right-click git bash, and add the remote address:
$ git remote add origin [email protected].com:yourName/yourRepo.git
Yourname and yourrepo indicate your GitHub user name and the newly created repository. git: Open config. Here, there will be an additional remote "origin" content, which is the remote address just added. You can also directly modify config to configure the remote address.
4. Submit and upload
Next, add some files in the local repository, such as README,
$ git add README
$ git commit -m "first commit"
Upload to GitHub:
$ git push -u origin master
If you encounter a non-fast-forward error, you need to handle it as follows:
The cause of the non-fast-forward problem is that the GIT repository already has some code, so it does not allow you to overwrite your code directly. Therefore, you have two options:
1. Strong pushing: Use your local code to replace the content in the GIT repository with strong coverage
Git push-F
2. First fetch the GIT stuff to your local location, then merge, and then push
$ Git fetch
$ Git merge
These two commands are equivalent
- $ Git pull
However, the following problem occurs again:
The [branch "master"] shown above must be clear (. Git/config) as follows:
[Branch "master"]
Remote = Origin
Merge = refs/heads/Master
This tells git2 things:
1. When you are in master branch, the default remote is origin.
2. If you use git pull on Master branch without specifying remote and branch, git uses the default remote (that is, origin) all the changes to merge on Master Branch
If you do not want or do not want to edit the config file, enter the following command line on Bush:
- $ Git config branch. master. Remote Origin
- $ Git config branch. master. Merge refs/heads/Master
Then run git pull again. Finally, git pushes your code. It works now ~
The GIT push command pushes the local repository to the remote server.
The GIT pull command is the opposite.
After the code is modified, you can use git status to view the differences between files. You can also use git add to add files to commit, or use git add-I to intelligently add files. After that, git commit submits this modification and git push uploads it to GitHub.
5. gitignore File
. Gitignore indicates the file to be ignored by git, which is a very important and practical file. Generally, after writing the code, we will perform compilation, debugging, and other operations. During this period, many intermediate files and executable files will be generated. These are not code files and do not need to be managed by git. We will see a lot of such files in git status. If you use git add-a to add them, it will be too troublesome to manually add them one by one. Then we need. gitignore. For example, in C # projects, my. gitignore is written as follows:
bin
*.suo
obj
BIN and OBJ are compiling directories, which are not source code and ignored. suo files are vs2010 configuration files and are not required. In this way, you will only see the source code file during git status, so you can safely add-a to git.
6. Tag
We can create a tag to point to a critical period in software development. For example, when the version number is updated, we can create a tag such as "V2.0" and "v3.1, in this way, it will be more convenient for future review. Tag is easy to use. The main operations include viewing tags, creating tags, verifying tags, and sharing tags.
6.1 view tags
List all tags:
git tag
The tags listed in this way are sorted alphabetically and do not matter when the tag is created. If you only want to view some tags, you can add restrictions:
git tag -l v1.*
In this way, only version 1. is listed.
6.2 create a tag
Create a lightweight Tag:
git tag v1.0
The created tag does not contain any other information, and corresponds to the tag with information:
git tag -a v1.0 -m ‘first version‘
-M is followed by the annotation information, which will be useful for future viewing. This is a common tag, and there is a signature tag:
git tag -s v1.0 -m ‘first version‘
The premise is that you have a private key for GPG, just replace a with S. In addition to adding tags for the current progress, we can also add tags for the previous commit:
# First view the previous commit
git log --oneline
# Assume there is a commit: 8a5cbc2 updated readme
# Add tags for him
git tag -a v1.1 8a5cbc2
6.3 Delete tags
Very easy. After knowing the Tag Name:
git tag -d v1.0
6.4 verify tag
If you have a GPG private key, you can verify the tag:
git tag -v v1.0
6.5 shared tag
When we execute git push, the tag will not be uploaded to the server. For example, after the tag is created on GitHub, the tag cannot be seen on the GitHub webpage, to share these tags, you must:
---------------------------------------------------------------
1. Basics
git config --global user.name "Your Name"Set your repository username (used to identify the submitter)
git config --global user.email "[email protected]"Set your repository email address (used to identify the submitter)
git initInitialize a git Repository
git add --allAdd all changed files
git add filename1Of course, you can specify to add filename1.
git commit -m "commit message"Add the changed information, which must be in place. Otherwise, an error is returned. We do not recommend that you do not add this information.
git statusView Current git status
git diff filename1View the modified content of filename1.
git logView recent submission logs
git log --pretty=onelineSingle Row display submission log
git reset --hard commitIDExploitationgit logThe returned commitid version
git reset --hard HEAD^Back to the previous version
git reflogTo view the command history, you can findgit logCommitid that you cannot see, becausegit logOnly the current commit log is displayed. If you submit the log once and regret it after you return the version, you can view the committed commitid.
git checkout -- filename1Replace files in the workspace with the version in the version library. Function 2:
- Undo File Modification in two cases:
- Undo changes in workspace (not used
git addCommand)
- Undo the modification in the temporary storage area (added to the temporary storage area and modified)
- Retrieve deleted files
- Deleted files in the workspace by mistake. You can use this command to retrieve files from the repository.
git reset HEAD filename1Undo add and return to work zone
git rm filename1Delete an object
git remote add origin https://github.com/pengloo53/learngit.gitAssociate the local library with the GitHub remote Library
git push -u origin masterAdd-uParameter to associate the master branch of the local database with the master branch of the remote database.-u.
git clone https://github.com/pengloo53/learngit.gitClone remote database to local
2. Branch Management
git checkout -b devCreate a Dev branch and switch to Dev. Equivalentgit branch dev,git checkout devTwo commands.
git branchView Current Branch
git merge devMerge the specified branch to the current branch. For example, if you have a master branch, execute the command to merge the dev branch to the master branch.
git branch -d devDelete Dev Branch
git log --graph --pretty=oneline --abbrev-commitView branch merge chart
git merge --no-ff -m "merge with no-ff" devDisable "Fast forward", that is, keep branch information.
git stashStore the work zone on site, and continue to work after recovery. It is usually used to handle more urgent tasks, such as bugs.
git stash listView saved work sites
git stash applyResume Work Site
git stash dropDelete stash content
git stash popRestore and delete stash content directly
git stash apply [email protected]{0}Resume a specified job site when you save more than one job site.
git branch -D feature-vulcanForcibly delete a branch. It is used when merge or local deletion is not required.
git remoteView the remote database information. Generally, the origin is returned.
git remote -vView the details of the remote database.
git push origin masterPush the local master branch to the remote master branch.
- The master branch is the main branch, so it must be synchronized with the remote at any time;
- The Dev branch is a development branch and all team members need to work on it, so they also need to be remotely synchronized;
- The bug branch is only used to fix bugs locally and does not need to be pushed to a remote device;
- Whether the new feature branch of feature is pushed to a remote device depends on whether you work with others to develop it.
git clone https://github.com/pengloo53/learngit.gitClone the remote database to the local machine. By default, only the master branch is displayed.
git checkout -b dev origin/devCreate remote Dev branch to local
git pullCapture the latest content of the remote branch.
git branch --set-upstream dev origin/devCreate a link between the local Dev Branch and the remote Dev branch.
Collaborative Working Mode
- First, you can try to use
git push origin branch-namePush your own changes;
- If the push fails, you must first use git pull to try to merge the local updates because of the remote distribution ratio (if git pull prompts "no tracking information ", the link between the local branch and the remote branch is not created.
git branch --set-upstream branch-name origin/branch-name);
- If a merge conflict exists, the conflict is resolved and submitted locally;
- After no conflict or resolution, use
git push origin branch-namePush is successful!
3. Tag Management
git tag v1.0Tag The current Branch
git tagView All tags in chronological order.
git log --pretty=oneline --abbrev-commitScale down the commitid and display the submission information in a single row
git tag v0.9 commitIDRun the previous command to view the commitid and then tag it. It is used to forget to tag, because the tag actually only needs a commitid pointer. By default, the tag is placed on the latest submission.
git show v0.9View the tag information.
git tag -a v0.1 -m "version 0.1 released" commitIDCreate a label with instructions,-aTag Name,-mDescription text.
git tag -d v0.1Delete tag v0.1
git push origin v1.0Push tag 1.0 to remote
git push origin --tagsPush all tags to remote
git push origin :refs/tags/v0.9To delete a remote tag, you must first Delete the tag locally.
4. Customize git
git config --global color.ui trueShow git colors
.gitignoreEdit the file you want to ignore in this file and submit it to git to ignore the check of special files. For example*.dbWrite.gitignoreAll dB files are ignored. Refer to all. gitignore collected by GitHub.
git config --global alias.st statusSet the alias of status to st.git st=git status.
git config --global alias.unstage ‘reset HEAD‘Sogit reset HEAD filename=git unstage filename
git config --global alias.last ‘log -1‘Knockgit lastThe last submission is displayed.
5. Build a git Server
sudo apt-get install gitInstall git;
sudo adduser gitAdd a git user;
sudo git init --bare sample.gitInitialize the GIT repository;
sudo chown -R git:git sample.gitChange the repository user to git;
- Set git user information
git:x:1001:1001:,,,:/home/git:/bin/bashChangegit:x:1001:1001:,,,:/home/git:/bin/git-shellTo disable shell logon.
git clone git@server :/director/sample.gitClone the repository on the GIT Server