say in front:
When I first started working with Git in an internship, it was a git visualizer, but it was limited to cloning libraries, switching branches, crawling, and pushing, not knowing the fundamentals. Read the Liaoche teacher git tutorial, the benefits are quite abundant. In particular, thanks to my colleagues at work, he strongly advised me to use the command line and to guide and explain me in my work, so I realized the fun of using the command line and the powerful features of Git. In order to not be despised by him next week, I have to work hard! The following command line summary is through the study of teacher Liao's tutorial summed up, in order to facilitate their own view ~ ~
git Command line summary:
To create a version library
mkdir
: Create an empty directory in the current directory, which is an empty git repository
pwd
: Displays the current directory
git init
: Initialize a repository, turn the current directory into a repository that git can manage (the resulting .git
directory, which is git to track the management repository, is hidden by default)
ls -ah
: Show hidden directories
git add
: Add File to warehouse buffer (INDEX), save changes temporarily
git add *
: Add all files to the buffer
git commit -m "wrote a readme file"
: Submits the file to the repository (HEAD), pointing to the result of the most recent commit, but not to the remote repository. The -m
following is a description of this submission
- Why does Git need
add
, commit
two steps? : Because commit
many files can be submitted (submitted to the warehouse) at one time, add
different files (added to the buffer) can be
Time Machine Shuttle
git status
: View Results
git diff
: View the modified content
1) version fallback
git log
: Displays the commit log from the most recent to the farthest (with the version number, which is different for commit id
everyone)
git log --pretty=oneline
: Displays a simple commit log
git reset --hard HEAD^
: Fallback to previous version (pointing to the current version, pointing to the last version HEAD
HEAD^^
, pointing to the HEAD~100
previous 100 versions)
git reset --hard commit-id
: Revert to a version ( commit id
do not have to write all, write the first few can)
git reflog
: Records every command of an operation
2) Working area and staging area
Workspaces: Directories on your computer, repositories: Hidden .git
directories, git repositories with a lot of stuff, the most important of which is the staging area called Stage (index), the first branch that git automatically creates for us master
, and a pointer master
to One of the pointers is calledHEAD
3) Management changes
cat readme.txt
: View File Contents
git diff HEAD -- readme.txt
: See the difference between the latest version of the workspace and the repository
- Each change, if not
add
to staging area, it will not be added to the commit
4) Undo Changes
git checkout -- file
: Discard changes to workspace, undo all changes to file in workspace
git reset HEAD file
: Undo the staging area changes and re-put them back into the work area.
git reset
: You can either roll back the version or staging area The changes back to the workspace. When we use HEAD
it, it means the latest version.
5) Delete Files
rm file
: Workspace Delete Files
git rm file
: Delete the file from the repository
git checkout -- file
: Replaces the workspace version from the version in the repository, which can be "one-click Restore" Regardless of whether the workspace is modified or deleted
Remote Warehouse
Git is a distributed version control system, and the same git repository can be distributed across different machines.
ssh-keygen -t rsa -C "[email protected]"
: Create SSH Key in user's home directory
.ssh
There are id_rsa
id_rsa.pub
two files in the directory, these two are SSH key key pair, is the id_rsa
private key, can not be disclosed, id_rsa.pub
is the public key, may be assured to tell others
- Why does GitHub need ssh Key? : Because GitHub needs to recognize that the submission you pushed is really something you pushed, not someone else impersonating it, and Git supports the SSH protocol, so if GitHub knows your public key, you can confirm that only you can push it.
- GitHub allows you to add multiple keys on different computers.
1) Add remote library (first local library, then remote library)
git remote add origin [email protected]:userName/learngit.git
: Associates a local warehouse with a remote library. origin
: The name of the remote library, which is the default for git.
git push
: Pushes the current branch master
to the remote
git push -u origin master
: When you push a branch for the first time, master
because the remote library is empty, plus -u
parameters, Git will not only push local branch master
content to a new branch of the remote master
, but also associate the local branch master
with the remote master
branch. , you can simplify the command at a later push or pull.
git push origin master
: After the first push, you can use this command to push the master
latest changes in the local branch to GitHub
- One of the biggest benefits of a distributed version system is that working locally does not have to take into account the existence of a remote library, that there is no internet connection, and that SVN is not able to work without networking.
- Tip error Message: Fatal:remote Origin already exists.--> solution:
git remote rm origin
2) from remote Couclon (first remote library, then clone to local)
git clone [email protected]/userName/gitskills.git
: Cloning a local library
- GIT supports a variety of protocols, the default
git://
using SSH, the fastest, but also can use https
other protocols.
- With the
https
exception of slow speed, one of the biggest problems is that each push must enter a password, but in some companies that only open the HTTP port, the protocol cannot be used ssh
and can only be usedhttps
Branch Management
- Git's branches, whether created, toggled, or deleted, can be completed within 1 seconds. Whether your repository is 1 files or 10,000 files!
1) created in Merge branch
master
is the main branch; a HEAD
point, that is, the master
current branch; master
point to commit, each commit, the master
branch moves forward one step, so that as you continue to commit, master
the lines of the branch are getting longer.
- Create a new branch
dev
: Git creates a new pointer called dev
, points to master
the same commit, and then HEAD
points dev
to it, indicating that the current branch is dev
on.
- Merge branch: The
master
dev
current commit that points to.
- After merging the branches, you can delete the
dev
branches, that is, to dev
delete the pointer, leaving only one master
branch
git branch
: To view the current branch, all branches are listed, and the current branch is preceded by a *
number
git branch <name>
: Creating a Branch
git checkout <name>
: Toggle Branch
git checkout -b <name>
: Create <name>
a branch and switch to a <name>
branch (create + toggle Branch)
git merge <name>
: Merges the specified branch () to the current branch ( master
) (first to switch to the master
Main Branch as the current branch)
git branch -d <name>
: Delete Branch
2) Conflict resolution
git log --graph
: View Branch Merge diagram
git log --graph --pretty=oneline --abbrev-commit
: View Simple branching merge diagram
- When git cannot merge branches automatically, you must resolve conflicts first. After resolving the conflict, submit again, and the merge is complete.
- Git
<<<<<<<
, =======
to mark the >>>>>>>
contents of different branches
3) Branch Management policy
Typically, when merging branches, GIT uses patterns when possible, Fast forward
but in this mode, the branch information is discarded after the branch is deleted.
If you want to force the mode to be disabled, Fast forward
git will generate a new commit at merge, so you can see the branching information from the branch history.
git merge --no-ff -m "merge with no-ff" <name>
: no-ff
parameter indication is disabled Fast forward
, can be combined with normal mode, the merged history has branches, can see that has been merged.
In the actual development, we should follow several basic principles of Branch Management:
(1) Master branch: should be very stable, that is, only to release the new version, usually can not work on it;
(2) Dev branch: Used to work, the Dev branch is unstable, at some point, such as 1.0 release, then merge the Dev branch to master, the Master Branch Release 1.0 version;
(3) You and your friends each work on the Dev branch, and everyone has their own branch, and it's time to merge on the Dev branch.
4) Bug Branch
- In Git, each bug can be fixed by a new temporary branch, after the fix, merge the branch, and then delete the temporary branch.
git stash
: The current work site "storage", and so on after the resumption of the site to continue to work
git stash list
: View the job site just now
git stash apply
: Reply to work site
git stash drop
: Delete Stash Content
git stash pop
: The stash content was deleted at the same time.
git stash apply [email protected]{0}
: Reply to the specified stash
5) Feature Branch
git branch -D <name>
: Forcibly Delete a branch
- To develop a new feature, it is better to create a new branch;
- If you want to discard a branch that has not been merged, you can forcibly delete it by command
6) Multi-person collaboration
- When you clone from a remote repository, git actually automatically corresponds to the local
master
branch and the remote master
branch, and the default name of the remote repository isorigin
git remote
: Viewing remote libraries
git remote -v
: View more detailed remote library information
git push origin branch-name
: Pushes a branch to push all local commits on that branch to the remote library
- Branches that require time synchronization with remote:
master
Main Branch and dev
Development Branch
git checkout -b dev origin/dev
: Create origin
a remote dev
branch to a local
git pull
: Fetching branches from remote
git branch --set-upstram dev orign/branch-name
: If git pull
"No tracking information" is indicated, the link relationship between the local branch and the remote branch is not created, and the command is used to set up branch-name
and origin/branch-name
link
Label management 1) Creating labels
git tag <name>
: Used to create a new label, default to HEAD
, or specify a commit ID
git tag <name> <commit-id>
: tag on the corresponding commit ID
git tag -a <tagname> -m "blablabla..." 3628164
: Specify label information, -a
specify label name, -m
specify descriptive text
git tag -s <tagname> -m "blablabla..." 3628164
: -s
sign a label with the private key, you can use the PGP signature tag
git tag
: View All Tags
git show <tagname>
: View label information
- The default tag is on the latest commit
- Labels are not listed in chronological order, but are sorted alphabetically
2) Operation label
git tag -d <tagname>
: Delete a label
git push origin <tagname>
: pushes a local tag to a remote
git push origin --tags
: A one-time push has not been pushed to a remote local label
git push origin :refs/tags/<tagname>
: Delete a remote label
customizing git
git config --global alias <alias> <command>
: Aliases the command <command>
<alias>
- When you configure Git, plus
--global
it works for the current user, if not, it only works for the current warehouse.
- Each warehouse configuration file is placed in a
.git/config
folder
Reference: Liaoche's official website--git Tutorial Git-cheatsheet.pdf
git command collation