Git commands to configure Study Notes

Git notes

  • Git is a distributed version control system
  • Centralized VS distributed:
    1. Centralized version control systems, centralized repository stored in a central server, it must be networked to work, no historical repository.
    2. Distributed version control system version control system, there is no "central server", everyone on the computer is a complete repository.
    3. Distributed System advantages: higher security, no network, if the central server fails, any other local developers have a new repository with history.
  • The main difference is that the storage of historical repository, centralized system exists only in the version history of a central server, and distributed control systems in each local library has a history storage.

Git commands

Git Configuration

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"  

Note that git configthe command --globalparameter, use this parameter, all Git repository that you will use this configuration on this machine, of course, you can specify a different user name and Email address of a warehouse.

Create Repository

Create a git repository in the current directory

$ git init 

To add files to the repository

$ git add <file>
$ git commit

git addCan be used repeatedly to add multiple files, git commityou can submit many files at once, in the git commitAdd command (-m '····') easily modify records from the history list to find.

Grasp the state of the workspace

$ git status 

View file content

$ git diff

Version rollback

$ git reset -- hard HEAD^

HEAD points version is the current version back to the previous version using the above command, if the rollback on the use of two versions HEAD^^, if the fallback version number is large (such as up 50 version), use HEAD~50.

Specified version rollback

$ git reset --hard commit_id

commit_idThe version number is specified, it is calculated by the SHA1 digital

View submission history

$ git log   

View command history

$ git reflog

Work area, staging area and repository

git is different from the other version control systems is the concept of the staging area, the work area is the computer can be seen in the catalog, the work area has a hidden directory [.git], this is the git repository. The repository there are many things, the most important is the staging area called the stage.

When you add a file to the repository is performed in two steps:

  1. The first step is git addto add into the file, the file is actually a modification added to the staging area.
  2. The second step is git committo submit changes, in fact, is to submit all the contents of the staging area to the current branch.

Git is how to track changes, each modified, if not git add to the staging area, it will not be added to the commit.

Undoing Changes

Modify discard the workspace

$ git checkout -- <file>

The command is working to undo all the documents changes, there are two cases:

  • One is the file since the modification has not been placed in temporary storage area, and now, back and undo changes to the repository exactly the same state;
  • One is the file has been added to the staging area, has been modified and now returns to undo changes added to the state after the staging area.

In short, it is to make the file back to the state at the time of the last git commit or git add.

Discard changes to the temporary area

Change the contents of a file chaotic workspace also added to the staging area, when you want to discard changes, first use the command git reset HEAD <file>, then press the revocation of the work area to modify the operation.

Was amended commit command of

Has submitted improper modifications to the repository, you want to undo changes, use the version rollback command, the premise is not pushed to the remote repository.

Delete Files

$ git rm <file>

When you want to delete a file text.txt, you can employ the command: rm test.txt
this time there are two cases:

the first case: indeed want to test.txtdelete, you can perform
$ git rm test.txt
$ git commit -m "remove test.txt"
this time a file is deleted, and delete records upload local library.

The second case: accidentally deleted files and want to restore, this time has not been commit -m "remove test.txt"executed git checkout test.txtto restore files.

If you perform complete git commit -m "remove test.txt"after can not be used checkoutto restore, starting git reset --hard HEAD^again from the repository to write back to the work area.
git rmIt used to delete a file. If a file has been submitted to the repository, so you never have to worry about mistakenly deleted, but be careful, you can only restore files to the latest version, you will lose the contents of the most recent commit your changes.

Remote repository

Creating SSH Key

$ ssh-keygen -t rsa -C "[email protected]"

Associated with the remote warehouse

$ git remote add origin https://github.com/username/repositoryname.git

Pushed to the remote repository

$ git push -u origin master

-u represents the first push all the contents of the master branch, then, after each local commit as long as necessary, you can use the command $ git push origin masterto push the latest changes.

From remote cloning

$ git clone https://github.com/usern/repositoryname.git

Note: When you can not use [email protected]commands to push and cloning, because there is no key is installed.

  • Adding to enjoy privacy key$ ssh-add ~/.ssh/id_rsa
  • If you add can fail to execute commands $ eval \ssh-agent\is the symbol on the ~ key, and then add the privacy of the key again.
  • Use $ ssh -T [email protected]is bound to determine success. If the return successfullyon success.

Branch Management

View branch

$ git branch

Creating a branch

$ git branch <name>

Switching branch

$ git checkout <name>

+ Create a switch branch

$ git checkout -b <name>

Merge a branch to the current branch name

$ git merge <name>

Deleted branches

$ git branch -d <name>

Forcibly remove the branch

$ git branch -D <name>

Not to discard a branch had been combined, it can be implemented by the above command.

View branch merged graph

$ git log --graph

When Git does not automatically merge branches, we must first resolve the conflict. Post-conflict resolution, and then submit the merger is completed.
With the git log --graphcommand to see the branch merged graph.

Normal mode merging branches

$ git merge --no-ff -m "description" <branchname>

Normally branch merge, git will use the Fast forward mode, delete the branch, the branch information is lost, you can use --on-ffparameters, disable Fast forward, if necessary add a -mparameter to commit description written into it. After this history merge with branches, you can see it once did merge.

Save your work site

$ git stash  

Check job site

$ git stash list

Restoration work site

$ git stash pop

Recommended operation of bug fixes in the new branch

First sort out the two concepts:
untracked files: it refers to the new file or folder and not added to the staging area (the new has not yet been git add)
is not added to the file in the temporary area: Refers that have been tracked before, but not added to the staging area (already performed git add / commit but after this amendment has not git add)
example:
readme.md have been git add / git commit too, but then, I the only modification, and no modifications done, can not the commit;
the Test new folder, not git add / git commit;
there is a need to deal with bug, this time I need to switch branches, to deal with the bug
right steps:
git the Add test (git to track the new file)
git stash reserved spot
if you do not execute these two commands, then git status after modifying the BUG complete, you will find readme.md not added to the staging area, while the addition of a test file,
but did not complete their readme.md, absolutely do not submit, which causes the bug to modify the code can not be submitted.
So need git stash, so submit bug fixes in the code, you will not see readme.md and test. You can safely submit bug fixes code.

View remote library information

$ 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

$ git push origin branch-name

If push failed to grab the remote with a new git pull submitted;

Creating local and remote branch corresponding branch

$ git checkout -b branch-name origin/branch-name

Name of the local and remote branch of the best consistent;

Associate local branch and a remote branch

$ git branch --set-upstream branch-name origin/branch-name

Grab from a remote branch

$ git pull

If there is a conflict, it must first deal with conflict.

label

git tag is a snapshot of the repository is in fact a commit pointer, if you want to find a particular version commit numbers, complex numbers easy to find, use the tag to take a name easy to remember and understand a lot easier, it is with a a commit tied together. (Analogous to the relationship between domain names and IP addresses?)

New Label

$ git tag<tagname> 

The default is HEAD, you can also specify a commit id.

Look at the label information

$ git show <tagname>

Create a label with instructions

$git tag -a <tagname> -m <description> <branchname> or commit_id

Specify the label name with -a, -m specify caption

View all tags

$ git tag

Push a label to a remote

$ git push origin <tagname>

Full-time push yet pushed to the remote local label

$ git push origin --tags

To delete a local label

$ git tag -d <tagname>

To delete a remote tag

$ git push origin :refs/tags/<tagname>

Guess you like

Origin www.linuxidc.com/Linux/2020-02/162404.htm