Git

Git: distributed version controller

SVN: Centralized version controller (Must be networked)

download

Git

Git operation flow

  1. The workspace: a place on the local computer to store project files, such as the learnGitProject folder;
  2. Staging area (Index/Stage)When you use git to manage project files, you can add a.git folder to your project file. Git folder contains two parts, one is the staging area (Index or Stage), which is the temporary storage of files, usually use the add command to add files from the workspace to the staging area.
  3. Local repositoryThe git folder also contains the master branch that git automatically creates, and the HEAD pointer points to the master branch. You can use the commit command to add files from the staging area to the local repository.
  4. Remote warehouse: Not in the local repository, the project code is on the remote Git server. For example, the project is on Github, which is a remote repository. Usually, clone command is used to copy the remote repository to the local repository and push it to the remote repository after development.

SSH configuration

  1. Open theiTerm2.
  2. usecd ~/.sshYou can check whether SSH is configured.
  3. Execute commands to generate public and private keysssh-keygen -t rsaAnd press Enter 3 (why press enter 3, because there is a prompt to ask you whether you need to set the password, if you set the password every time you use Git, generally do not write blank, just enter). A private key is generated in a folderid_rsaAnd a public keyid_rsa.pub.
  4. SSH If no special operation is performed, it is generally stored in the /Users/admin/. SSH directory. If you cannot see the.ssh file, use itshift+cmd+.This is the file where the secret keys are stored. Open this file and you will see id_rsa and id_rSA.pub. Id_rsa is a private key file, and id_rsa.pub is a public key file.
  5. Run the command to view the public keycat ~/.ssh/id_rsa.pub.

Git Configuration Commands

Querying Configuration Information

  1. List the current configuration:git config --list;
  2. List the repository configuration:git config --local --list;
  3. List global configuration:git config --global --list;
  4. List the system configuration:git config --system --list;

Configure user information for the first time using Git

  1. Configure the user name:git config --global user.name "your name";
  2. Configuring user email addresses:git config --global user.email "[email protected]";

Other configuration

  1. Configure which difference analysis tool to use when resolving conflicts, for example to use Vimdiff:git config --global merge.tool vimdiff;
  2. Configure git command output to be colored:git config --global color.ui auto;
  3. Configure git with a text editor:git config --global core.editor vi;

Action commands on the workspace

The new warehouse

  1. To manage project files in your workspace using Git, create a new local repository:git init;
  2. Copy projects from remote Git repositories:git clone <url>, such as: git clone git://github.com/wasd/example.git; If you want to define a new project name when cloning a project, you can specify a new project name after the clone command:git clone git://github.com/wasd/example.git mygit;

submit

  1. Commit all files in your workspace to staging:git add .
  2. Commit the specified file in the workspace to staging:git add <file1> <file2> ...;
  3. Commit all files in a folder in the workspace to staging:git add [dir];

undo

  1. Delete the workspace file, and also delete the record of the corresponding file from the staging area:git rm <file1> <file2>;
  2. Delete file from staging area, but workspace still has the file:git rm --cached <file>;
  3. To cancel files that have been temporarily stored in the staging area:git reset HEAD <file>...;
  4. To undo the last operation on a file:git checkout --<file>. Stashing and branching can be used to determine that a previous change to a file is no longer needed, if you want to save the previous change for later work.
  5. Hide current changes so you can switch branches:git stash;
  6. View all current stores:git stash list;
  7. Apply the latest storage:git stash applyIf you want to apply earlier storage:git stash apply stash@{2}; Reapply temporary changes that need to be added--indexParameters:git stash apply --index;
  8. Using the apply command simply applies the store while the content is still on the stack, removing the specified store:git stash drop stash{0}; If you use the pop command, you can not only reapply the store, but also clear it from the stack immediately:git stash pop;
  9. In some cases, you may want to apply changes to the store and then undo the changes you applied to the store after making some other changes. Git does not provide a command similar to Stash unapply, but the same effect can be achieved by undoing the stash patch:git stash show -p stash@{0} | git apply -R; Also, if you don’t specify a specific store, Git will select the nearest store:git stash show -p | git apply -R;

Update file

  1. Rename the file and submit the renamed file to the staging area:git mv [file-original] [file-renamed];

Novelty search information

  1. Query the status of all files in the current workspace:git status;
  2. Git diff; git diff; git diff; git diff; git diff. Specify file differences between workspace and staging:git diff <file-name>;

Ignore file. Gitignore

There will always be files that don’t need to be managed by Git and that you don’t want to end up in the list of untracked files. These are usually automatically generated files, such as log files, or temporary files created during compilation. We can create a file named.gitignore that lists the file modes to ignore. The following cases:

Git will ignore all.a files *. A # except lib. Lib. a # Just ignore the TODO files in the project root directory, Build / # ignores doc/notes.txt but not doc/server/arch.txt doc/*.txt # ignores doc/ All TXT files in the directory are doc/**/*.txtCopy the code

Git Basic Operations

git add

Add files to the staging area

Add a file to the staging area, which can be followed by multiple files, separated by Spaces
git add xxx
Add all currently changed files to the staging area.Git add. copy the codeCopy the code

git commit

# Submit temporary changes, a new editor will be opened for editing
git commit 
Commit the temporary changes and record the remarks
git commit -m "you message"
Git add. && git commit -m
git commit -am
This operation modifies the hash value of the last commitGit commit -- Amend copies the codeCopy the code

git pull

Git pull = git fetch && git mergeGit pull < remote host name > < remote branch name ># use rebase to mergeGit pull --rebase < remote host name > < remote branch name >:< local branch name > copy codeCopy the code

git fetch

Unlike Git pull, git fetch only pulls remote changes and does not automatically merge. Has no effect on your current code

Get updates for a specific branch of the remote repositoryGit fetch < remote host name >Get updates for all branches of the remote repositoryGit fetch --all copies codeCopy the code

git branch

Create a local branch without switching
git branch <branch-name> 
# View local branches
git branch
# View remote branches
git branch -r
# View local and remote branches
git branch -a
# delete local branch
git branch -D <branch-nane>
Rename the branch
git branch -m <old-branch-name> <new-branch-name>
Copy the code

Linux command

CD: Changes the directory

cd .. : Returns to the upper-level directory

Touch: Creates a file

Ls /ll: lists all files in the current directory.

PWD: Displays the current directory path

Mkdir: creates a folder

Rm -r: Deletes a folder

Mv: Move a file

Reset: initializes the terminal

The clear: clear screen

History: Displays the history command

Exit, exit

Comments # :

References:

Git basic operation, one article is enough!

How do I use Git in my work