Git
Git: distributed version controller
SVN: Centralized version controller (Must be networked)
download
Git
Git operation flow
The workspace
: a place on the local computer to store project files, such as the learnGitProject folder;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.Local repository
The 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.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
- Open the
iTerm2
. - use
cd ~/.ssh
You can check whether SSH is configured. - Execute commands to generate public and private keys
ssh-keygen -t rsa
And 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. - 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 it
shift+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. - Run the command to view the public key
cat ~/.ssh/id_rsa.pub
.
Git Configuration Commands
Querying Configuration Information
- List the current configuration:
git config --list
; - List the repository configuration:
git config --local --list
; - List global configuration:
git config --global --list
; - List the system configuration:
git config --system --list
;
Configure user information for the first time using Git
- Configure the user name:
git config --global user.name "your name"
; - Configuring user email addresses:
git config --global user.email "[email protected]"
;
Other configuration
- Configure which difference analysis tool to use when resolving conflicts, for example to use Vimdiff:
git config --global merge.tool vimdiff
; - Configure git command output to be colored:
git config --global color.ui auto
; - Configure git with a text editor:
git config --global core.editor vi
;
Action commands on the workspace
The new warehouse
- To manage project files in your workspace using Git, create a new local repository:
git init
; - 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
- Commit all files in your workspace to staging:
git add .
- Commit the specified file in the workspace to staging:
git add <file1> <file2> ...
; - Commit all files in a folder in the workspace to staging:
git add [dir]
;
undo
- Delete the workspace file, and also delete the record of the corresponding file from the staging area:
git rm <file1> <file2>
; - Delete file from staging area, but workspace still has the file:
git rm --cached <file>
; - To cancel files that have been temporarily stored in the staging area:
git reset HEAD <file>...
; - 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. - Hide current changes so you can switch branches:
git stash
; - View all current stores:
git stash list
; - Apply the latest storage:
git stash apply
If you want to apply earlier storage:git stash apply stash@{2}
; Reapply temporary changes that need to be added--index
Parameters:git stash apply --index
; - 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
; - 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
- Rename the file and submit the renamed file to the staging area:
git mv [file-original] [file-renamed]
;
Novelty search information
- Query the status of all files in the current workspace:
git status
; - 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