Git_ base command

gitinit // initialize a Git repository "role =" presentation "> gitinit // initialize a Git repository gitinit // initialize a Git repository git status // check the status of the warehouse

gitadd.// add all modifications to the staging area "role =" presentation "> gitadd .// add all modifications to the staging area gitadd.// add all modifications to the staging area git add * // Ant style adding modified

All modifications gitadd * Controller // Controller will be the end of the file to the staging area "role =" presentation "> gitadd? All modifications Controller // Controller will be the end of the file to the staging area gitadd? Controller // All modifications will add files to the Controller at the end of the temporary area git add Hello * // to modify all the files that begin with Hello added to the staging area for example: HelloWorld.txt, Hello.java, HelloGit.txt ...

? GitaddHello // Hello will be back only the beginning of a revised document submitted to the staging area for example: Hello1.txt, HelloA.java or if it is HelloGit.txt Hello.java will not be added multiple times and then add submitted to the staging area in the modifications submitted "role =" presentation "> gitaddHello // Hello back at the beginning of the file will be only one example:? Hello1.txt, HelloA.java HelloGit.txt or if it is not Hello.java will be added can be added multiple times and then will submit gitaddHello // Hello behind only the beginning of a revised document submitted to the staging area for example:? Hello1.txt, HelloA.java or if it is HelloGit.txt Hello.java will not be added can be added multiple times and then submit git commit -m "comment" // will be submitted to modify the staging area to the rear of the warehouse to add information on the meaningful gaze

gitdifffile // been modified time has not yet submitted a modified portion of the view (and different diff latest version of the repository == different difference) in the file "role =" presentation "> gitdifffile // has not been modified in the file submitted when viewing the modified part (and different diff latest version of the repository == different difference) gitdifffile // it has been modified to view the modified part (and in the repository different diff latest version of the time has not yet submitted file == different difference) git log // View git commit information, the information submitted by each include gaze into account, from the oldest to the newest submit submit

gitlog-pretty = oneline // simplified commit information displayed in a row attention Git version number (commitid is) is the SHA1 ea34578d5496d7dd233c827ed32a8cd576c5ee85 "role =" presentation "> gitlog-pretty = oneline // simplified commit information displayed in a row of attention Git version number (commitid is) is the SHA1 ea34578d5496d7dd233c827ed32a8cd576c5ee85gitlog-pretty = oneline // simplified commit information displayed in a row attention Git version number (commitid is) is the SHA1 ea34578d5496d7dd233c827ed32a8cd576c5ee85 git reset -hard HEAD ^ // retracted with respect to the current version of the The current version represents a version HEAD

git reset -hard HEAD ^^ // return relative to a version HEAD representatives on the current version of the version of "role =" presentation "> return current gitreset-hardHEAD ^^ // relative to the previous version to the current version of HEAD represents the current version gitreset-hardHEAD ^^ // return relative to a version of the current version of the HEAD representative of the current version of git reset -hard HEAD ~ 100 // return to 100 with respect to the version of the current version to the current version represents HEAD

Gitreset-hard3628164 // return to the specified version does not require all written here commitidGit back automatically adapts to Git version rollback speed is very fast, because Git inside there is a point to the current version of the HEAD pointer when you roll back version of the time, Git is just from the HEAD point appendGPL "role =" presentation "gitreset-hard3628164 // return to the specified version> there is no need to write all commitidGit back automatically adapts to Git version rollback speed is very fast, because there is within Git HEAD pointer points to the current version, when you roll back version of the time, Git just returned from the HEAD appendGPLgitreset hard3628164-// pointing to a specific version there is no need to write all commitidGit back automatically adapted version rollback speed is very fast Git because Git inside there is a point to the current version of the HEAD pointer when you roll back version of the time, Git is just the HEAD commit log from the previous point appendGPL git reflog // older versions can be viewed after the return to the old version

When we want to return from an old version to the new version but we closed the shell window, you can not view before the commit id, you can

Previous versions of commitidgitreflog previous version before gitreflog view of commitid "role =" presentation "> gitreflog View to view the version of commitid git reset -hard 3628164

Work area and staging area

Workspace: it is that all files codebase we pass gitinit created but does not include the .git file (repository) staging area: All of our code base file by "role =" presentation "> gitinit created but does not include the .git file (repository) staging area: All of our file code library created by gitinit but does not include the .git file (repository) temporary storage area: modification we git add ./*/*Xxx/Xxxx* added, are entered into the staging area, not visible to the naked eye can see the modified state by $ git status

What is a modification?

For example, you add a line, which is a modification,

To delete a row, it is a modified,

Changed some characters, but also a modification,

Delete some and added some, it is a modified,

Even create a new file, it is also considered a modification.

Modifications can only be committed in the future add to the staging area

In the file has been modified yet add time

gitcheckout-file // represents modified workspace retracted discarding original state (not including modified and added to the temporary area) and has been modified File added to the staging area, yet commit "role =" presentation "> gitcheckout-file / / discard represent modify the workspace to return the original state (not including modified and added to the temporary area) file has been modified and added to the staging area, yet commitgitcheckout-file // discard represent modify the workspace returned to its original state (not including modified and added to the temporary area) file has been modified and added to the staging area, yet commit git reset HEAD file // discard file has been added to modify the staging area represent the latest version of HEAD

If the file has been submitted to modify the local repository

$ Git reset -hard HEAD ^ // go back to the previous version

rmfile // from the file system to delete a file (or recorded in Git), "role =" presentation " > rmfile // delete a file from the file system (or recorded in Git), rmfile // delete a file from the file system (Git there are still records), git status Git will tell you when a file has been deleted

gitrmfile // delete from the Git repository files (files are deleted from the file system) "role =" presentation "> gitrmfile // remove a file from Git repository (files are deleted from the file system) gitrmfile // from Git repository delete files (delete files from the file system) git status normal

gitrmfile // delete from the git repository file "role =" presentation "> gitrmfile // gitrmfile delete files from the git repository git commit -m // delete files from the git repository" delete file "// submit delete

gitcheckoutfile // will want to recover from the git: error:. pathspec'README.txt'didnotmatchanyfile (s) knowntogit "role =" presentation "> gitcheckoutfile // want to recover from the git will appear: error: pathspec'README.txt 'didnotmatchanyfile (s) knowntogit.gitcheckoutfile // will want to recover from the git: error:. pathspec'README.txt'didnotmatchanyfile (s) knowntogit git reset -hard HEAD ^ // revert to a previous version of a file can be restored

rmfile // Delete Files "role =" presentation "in the file system> rmfile // delete a file in the file system rmfile // delete a file in the file system git checkout file // because it is deleted from the file system so you can checkout through restore

$ ssh-keygen -t rsa -C “[email protected]” // 创建SSH Key

Since this Key is not used for military purposes, so there is no need to set a password, so all the way round like,

If successful will generate a directory .ssh there id_rsa and id_rsa.pub two files in the user UserName directory

id_rsa is private and can not leak out, id_rsa.pub is the public key, you can tell others there is also the added public on github

Landing GitHub, open the "Account settings", "SSH Keys" page and click "Add SSH Key", fill in any Title, paste the contents of id_rsa.pub file in the Key text box

GitHub allows you to add multiple Key. Suppose you have several computers, you submitted a while, while submitting at home, as long as each computer Key are added to GitHub, you can push to GitHub on each computer in the

Adding remote libraries

Do global configuration

gitconfig–globaluser.name“zhangsan”" role="presentation">gitconfig–globaluser.name“zhangsan”gitconfig–globaluser.name“zhangsan” git config –global user.email “[email protected]

[email protected]: zhangsan / ylez.git // // addorigin add a remote repository is to add a remote repository //[email protected]:zhangsan/ylez.git is the address of the remote repository //[email protected] address of the host that we can GitLab you own git server // zhangsan your user name is your warehouse ///ylez.git name "role =" presentation "> [email protected]: zhangsan / ylez.git / / add a remote repository // addorigin is to add a remote repository //[email protected]:zhangsan/ylez.git address is the address of the remote host //[email protected] warehouse we can GitLab you own git server // zhangsan your user name is your warehouse ///ylez.git name [email protected]: zhangsan / ylez.git // // addorigin add a remote repository is to add a remote repository // git @ github. com: zhangsan / ylez.git address //[email protected] host address of the remote repository we can GitLab you own git server // zhangsan your user name is your warehouse name ///ylez.git git push -u origin master // this will Pushed to the master branch of the remote master branch

$ Git push -u origin dev // dev local switch to the local branch and then pushed to the remote dev branch

Cloning a remote repository to local

Git support multiple protocols, including https, but ssh native git protocol supported by the fastest.

$ Git clone [email protected]: zhangsan / test.git // speaking to clone a remote repository local

Each commit, Git regard they strung together a timeline, this timeline is a branch

In Git, there is a default main branch, that branch master

HEAD strictly speaking is not directed to submit, but point to the master, master is the point of submission, so, pointing to the current branch HEAD

master branch is a line, Git with master points to the latest submission, and then point to master HEAD

Each submission, master branch will move one step forward, so, as you continue to submit, master branch lines are getting longer and longer

Git built a pointer called dev, point to master the same submission, and then point to dev HEAD, it means the current branch on the dev

gitbranch // View all git branch "role =" presentation "> gitbranch // View all git branch gitbranch // View all branch git branch dev git // Create the dev branch

gitcheckoutdev // switch to an existing branch "role =" presentation "> gitcheckoutdev // switch to an existing branch gitcheckoutdev // switch to a branch git checkout -b dev // Create dev existing branches, and switches to dev branch

$ Git branch -d dev // delete dev branch

gitcheckoutmaster // switch to the main branch "role =" presentation "> gitcheckoutmaster // switched to the main branch is switched to the main branch gitcheckoutmaster // git merge dev // dev branch into the master branch

Updating d17efd8..fec145a

Fast-forward

readme.txt | 1 +

1 file changed, 1 insertion(+)

Fast-forward information, Git tells us that this merger is a "fast-forward mode" is directed directly to master the current dev submitted, so the merger is very fast

$ Git branch -d dev // delete dev branch

Now only remaining master branch, feeling nothing had yet happened

Conflict

$ git checkout -b feature1

Switched to a new branch ‘feature1’

Readme.txt modify the last line read:

Creating a new branch is quick AND simple.

gitaddreadme.txt" role="presentation">gitaddreadme.txtgitaddreadme.txt git commit -m “AND simple”

[feature1 75a857c] AND simple

1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout master

Switched to branch ‘master’

Your branch is ahead of ‘origin/master’ by 1 commit.

Git also automatically prompts our current master than the master branch to the remote branch ahead of a submission.

In the last line on the master branch readme.txt file with the following:

Creating a new branch is quick & simple.

gitaddreadme.txt" role="presentation">gitaddreadme.txtgitaddreadme.txt git commit -m “& simple”

[master 400b400] & simple

1 file changed, 1 insertion(+), 1 deletion(-)

$ git merge feature1

Auto-merging readme.txt

CONFLICT (content): Merge conflict in readme.txt

Automatic merge failed; fix conflicts and then commit the result.

$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 2 commits.

#

Unmerged paths:

(use “git add/rm …” as appropriate to mark resolution)

#

both modified: readme.txt // conflict

#

no changes added to commit (use “git add” and/or “git commit -a”)

Git is a distributed version control system.

Git is free software distributed under the GPL.

Git has a mutable index called stage.

Git tracks changes of files.

<<<<<<< HEAD

Creating a new branch is quick & simple.

Creating a new branch is quick AND simple.

feature1

Git marked with <<<<<<<, =======, >>>>>>> the contents of different branches, we save modified as follows:

Creating a new branch is quick and simple.

Submit:

gitaddreadme.txt" role="presentation">gitaddreadme.txtgitaddreadme.txt git commit -m “conflict fixed”

[master 59bc1cb] conflict fixed

We can also see the consolidation of the branch with git log with parameters:

gitlog–graph–pretty=oneline–abbrev−commit" role="presentation">gitlog–graph–pretty=oneline–abbrev?commitgitlog–graph–pretty=oneline–abbrev?commit git log –graph –pretty=oneline –abbrev-commit

* 59bc1cb conflict fixed

|\

| * 75a857c AND simple

* | 400b400 & simple

|/

* fec145a branch test

Delete feature1 branches:

$ git branch -d feature1

Deleted branch feature1 (was 75a857c).

Typically, when merging branches, if possible, Git will use Fast forward mode, but this mode, delete the branch, the branch will lose information.

If you want to force disable Fast forward mode, Git will generate a new commit in the merge, so, from the branch history information can be seen on branch

$ Git merge -no-ff -m "comment" dev // dev branch to merge, please note -no-ff parameters, disable Fast forward

$ Git log -graph -pretty = oneline -abbrev-commit // graphical representation bifurcation

* 7825a50 merge with no-ff

|\

| * 6224937 add merge

|/

* 59bc1cb conflict fixed

When the task of repairing a bug, code-named 101, it is natural that you want to create a branch issue-101 to fix it

In fixing a bug when hands have not completed the work but can not submit progress can be frozen

In your current work area (branch)

$ Git stash // freeze the current branch changes

See all the stash

gitstashlist // list all of the job site storage "role =" presentation "> gitstashlist // list all of the job site storage gitstashlist // list all of the job site storage git stash list

stash@{0}: WIP on dev: 6224937 add merge

gitstashapply (stash @ n) // resume work site (only one can be omitted when the latter, if there is more than one can restore the specified stash) but does not delete the store stash "role =" presentation "> gitstashapply (stash @n) // resume work site (only one can be omitted when the latter, if there is more than one can restore the specified stash) but does not delete the store stashgitstashapply (stash @ n) // resume work site (only one when the latter can be omitted, if there are multiple possible to recover for one stash) but does not delete stash git stash drop (stash {n} @ store) // delete the stored work site (can be omitted when only a back, and if you specify more than one deletion)

$ Git stash pop (stash @ {n}) // while restoring the contents of the stash is also deleted (only one can be omitted when the latter, but if you specify more than one deletion)

gitbranch-dbranchName // delete a branch, provided in the branch and the main branch of the branch produce has been merged (merge) "role =" presentation "> gitbranch? dbranchName // delete a branch, provided in the branch and generating the main branch of the branch have been merged (merge) gitbranch? dbranchName // delete a branch, provided in the branch and the main branch of the branch produce has been merged (merge) git branch -D branchName // delete a a branch, regardless of whether the merger will force the removal of

When you clone from a remote repository, in fact, Git automatically master branch and remote master branch of the association, and the default name of the remote warehouse is the origin.

To view information about a remote repository with git remote:

$ git remote

origin

Alternatively, the display of more detailed information git remote -v:

$ git remote -v

origin [email protected]:michaelliao/learngit.git (fetch)

origin [email protected]:michaelliao/learngit.git (push)

Not the local branch must take to remote push, then what needs to push branches, which do not need it?

master branch is the main branch, so to be synchronized with a remote time;

dev branch is the development branch, all team members need to work on it, so it needs to be synchronized with the remote;

branch bug fix for the bug only locally, there is no need to push the remote, unless the boss wants to see you every week in the end several bug fixes;

feature branch is expected to push the remote, depending on whether or not you and your little partner to develop on it.

When push if someone before you have to push, and push fails

[email protected]: michaelliao / learngit.git [rejected] dev-> dev (non-fast-forward) error:! failedtopushsomerefsto'[email protected]: michaelliao / learngit.git'hint: Updateswererejectedbecausethetipofyourcurrentbranchisbehindhint: itsremotecounterpart.Mergetheremotechanges ( eg'gitpull ') hint: beforepushingagain.hint: Seethe'Noteaboutfast-forwards'in'gitpush-help'fordetails because the latest submitted your little partner and you try to submit to push the conflict, the solution is simple, Git has. reminds us, first with the latest gitpull submit the catch down from origin / dev, then, in the local merger, conflict resolution, and push them up "role =" presentation "> [email protected]:! michaelliao / learngit.git [rejected] dev> dev (non fast forward??) error:? failedtopushsomerefsto'[email protected]: michaelliao / learngit.git'hint: Updateswererejectedbecausethetipofyourcurrentbranchisbehindhint: itsremotecounterpart.Mergetheremotechanges (eg'gitpull ') hint: beforepushingagain.hint:Seethe'Noteaboutfast? Forwards'in'gitpush-help'fordetails. Because the latest submitted your little partner and you try to submit to push the conflict, the solution is simple, Git has prompted us to use the latest submission from gitpull origin / dev catch down, and then, in the local merger, conflict resolution, and push them up [email protected]:! michaelliao / learngit.git [rejected] dev> dev error? (non fast forward??): failedtopushsomerefsto'git @ github .com: michaelliao / learngit.git'hint: Updateswererejectedbecausethetipofyourcurrentbranchisbehindhint: itsremotecounterpart.Mergetheremotechanges (eg'gitpull ') hint: beforepushingagain.hint: Seethe'Noteaboutfast forwards'in'gitpush-help'fordetails because the latest submitted your little partner?. and you try to submit the conflict to push, the solution is simple, Git has prompted us, first with the latest gitpull submit catch down from the origin / dev, then, in the local merger, conflict resolution, and push them up git pull // catch remote branches to submit to the localdev (?? non fast forward) error: failedtopushsomerefsto'[email protected]: michaelliao / learngit.git'hint: Updateswererejectedbecausethetipofyourcurrentbranchisbehindhint: itsremotecounterpart.Mergetheremotechanges (eg'gitpull ') hint: beforepushingagain.hint: Seethe'Noteaboutfast forwards'in? 'gitpush-help'fordetails. because the latest submitted your little partner and you try to submit to push the conflict, the solution is simple, Git has prompted us, first with the latest gitpull submit catch down from the origin / dev, then in local consolidation, conflict resolution, and push them up remote branch git pull // arrested and submitted to the localdev (?? non fast forward) error: failedtopushsomerefsto'[email protected]: michaelliao / learngit.git'hint: Updateswererejectedbecausethetipofyourcurrentbranchisbehindhint: itsremotecounterpart.Mergetheremotechanges (eg'gitpull ') hint: beforepushingagain.hint: Seethe'Noteaboutfast forwards'in? 'gitpush-help'fordetails. because the latest submitted your little partner and you try to submit to push the conflict, the solution is simple, Git has prompted us, first with the latest gitpull submit catch down from the origin / dev, then in local consolidation, conflict resolution, and push them up remote branch git pull // arrested and submitted to the local

No local dev branch and remote origin / dev branch of links, pushing will fail, so first we need to establish contact your local branch and a remote branch

$ Git branch -set-upstream dev origin / dev // dev establish local branches and remote branches dev Contact

Branch dev set up to track remote branch dev from origin.

to sum up:

View remote database information, use git remote -v;

The new local branch if you do not push the remote, that is not visible to others;

Push from the local branch using git push origin branch-name, if push fails to grab the remote with a new git pull submitted;

Create a corresponding remote branch and the local branch, using git checkout -b branch-name origin / branch-name, name of the local and remote branch is preferably the same;

Associated local branch and a remote branch, using git branch -set-upstream branch-name origin / branch-name;

Grab from a remote branch using git pull, if there is a conflict, we must first deal with conflict.

When you publish a version, we usually first make a tag in the repository, so that it uniquely identifies the moment of playing tag version.

Whenever the future, take a label version, is the version of history that will tag the time taken out. Therefore, the label is a snapshot of the repository.

Although the label is a snapshot of the Git repository, but in fact it is a pointer to a commit (with branches like right? But the branches can be moved, the label can not move), so, create and delete tags are instantaneous.

Labels are generally hit the master branch

gittagv1.0 // make a name on the current version is v1.0 HEAD tag "role =" presentation "> gittagv1.0 // make a name for v1.0 on the current version of the HEAD tags gittagv1.0 // make a name on the current version is v1.0 HEAD tag git tag // View all tags, labels will list all of the names

Playing tag history commit (and sometimes forget to tag the fight could have been the result of Monday Friday to think of it)

$ git log –pretty=oneline –abbrev-commit

6a5819e merged bug fix 101

cc17032 fix bug 101

7825a50 merge with no-ff

6224937 add merge

59bc1cb conflict fixed

400b400 & simple

75a857c AND simple

fec145a branch test

d17efd8 remove test.txt

$ Git tag v0.8 59bc1cb // to commit id to commit 59bc1cb ... the label marked v0.8

Tags are not listed in chronological order, instead of alphabetically. You can use git show to view label information

You can also create a label with instructions, with the specified tag name -a, -m specify the caption:

$ Git tag -a v0.1 -m "version 0.1 released" 3628164 // 3628164 ... is to commit id of the commit marked v0.1 label gaze content is version 0.1 released

gittagv0.1 // can view information including text labels can also be used by private -s signature label: "role =" presentation "> gittagv0.1 // can view information including text labels can also s? with a private key signature label: gittagv0.1 // can view information including text labels can also s with a private key signature label:? git tag -s v0.2 -m "signed version 0.2 released" fec145a

PGP signature using signature, therefore, you must first install the gpg (GnuPG), if not found gpg, or no gpg key pair, the error will be:

gpg: signing failed: secret key not available

error: gpg failed to sign the data

error: unable to sign the tag

If errors are reported, refer to the help documentation GnuPG configuration Key.

Git show command can see the PGP signature information:

$ git show v0.2

gittag-dv0.1 // remove tags v0.1 "role =" presentation "> gittag? dv0.1 // delete the label v0.1gittag? dv0.1 // remove tags v0.1 git push -u origin v1.0 // tag v1.0 will be pushed to the remote repository

$ Git push -u origin -tags // push all-time label

If the tag has been pushed to the remote, remote tag you want to remove a little trouble, delete start local:

$ Git tag -d v0.9

Deleted tag ‘v0.9’ (was 6224937)

Then, remove from the remote. Delete command also push, but the format is as follows:

$ git push origin :refs/tags/v0.9

To [email protected]: michaelliao / learngit.git

- [deleted] v0.9

.Gitignore create a special file in the root directory of your Git workspace, and then to ignore the file name into the fill, Git will automatically ignore these files

Ignore file principle is:

Ignore the operating system automatically generated files, such as thumbnails;

Ignored by the compiler generates intermediate files, executable files, etc., that is, if a file is automatically generated by another file that is automatically generated files into the repository is not necessary, such as Java compiled .class files generated;

Ignore your own configuration files with sensitive information, such as passwords stored in the configuration file.

A complete file .gitignore

Windows:

Thumbs.db

ehthumbs.db

Desktop.ini

project

*.class

*.war

classes

target

First, checkout the latest code is the default master branch of the main branch checkout code from git, so we need to checkout code development branch again

master branch is the latest stable version, while the dev branch is the latest development version

We must remember to create a new branch in the local checout from the server instead, if you do the new features in the previous two steps, checkout new sub-branch in the local branch dev

Then develop, submit the merger to dev, push the code to the server.

Guess you like

Origin www.cnblogs.com/zhichao123/p/11448495.html