A repository is a directory in which all files are managed by Git. Git can track every modification or deletion of a file so that it can be traced at any time in history or “restored” at any future point.

Must-read for programmers

Css3 coding techniques

Create and commit a repository

Mkdir create directory; PWD displays the absolute path. Note: All version control systems can only track changes to text files, such as TXT files, web pages, all program code, etc. Git is no exception. Binary files, such as images and videos, can also be managed by version control, but there is no way to keep track of the changes. Instead, the binary file can be linked to each change, so that the image is changed from 100KB to 120KB, but version control doesn’t know, and has no way of knowing.

Microsoft Word is a binary format, so there is no way for version control to track changes to a Word file. If you want to use version control, you have to write the file in plain text. Never use the Notepad that comes with Windows to edit any text file. The reason is that the Team that developed Notepad at Microsoft added the 0xefbbbf (hexadecimal) character at the beginning of every file, and you’ll run into a lot of incredible problems.

1. Core operations

The.git directory is hidden by default and can be displayed by the ls -ah command
git init

# 2. Add files to the repository. You can add more than one file at a time. The representative submitted all the documents in fullGit Add File Name File name# 3. Commit files to the current branch
git commit -m 'Note for This Submission'

# 4. Submit the project to (remote) storage
git push
Copy the code

2. Branch management

# 1. Look at the remote branch
git branch -a

# 2. Look at the local branch
git branch

# 3. Create and switch branches (add -b to create and switch branches)Git checkout -b Branch name# 4. Switch back to the branchGit Checkout branch name# 5. Create a new branch from the main branchGit checkout master -b branch name# 6. Merge a branch into the current branchGit merge branch name# 7. Delete the branch
git branch -dGit branch -d branch nameCopy the code

3. Resolve conflicts

# Scenario 1 conflict when committing or pulling
git stash
git pull
git stash pop

If you want to completely overwrite the local code and keep only the server-side code, go back to the previous version and pull again
git reset --hard HEAD^
git pull origin master
# Git origin master

When you merge a branch name in git3. Git add. (add changed file) 4'xj'(submitted)Copy the code

4. View the SSH key

Go to the. SSH directory
cd ~/.ssh
# 2. Check the id_rsa.pub file
cat id_rsa.pub
Copy the code

5. Clone remote projects

git cloneRemote source addressCopy the code

6. Roll back an operation

# 1. View command history
git reflog
# 2. Undo an operation according to commitIDGit reset --hard in a directoryCopy the code

7. Establish relationships with remote branches

Git branch -- set-ups-to =origin/ name of remote branchCopy the code

8. Delete files

Delete files from the repository
git rm filename
# Restore files deleted by mistake in the workspace
git checkout -- filename
Note: Git Checkout actually replaces the workspace version with the repository version. You can "restore" the workspace whether it is modified or deleted.
Copy the code

9. Remote storage

# 1. Create sshkey key
ssh-keygen -t rsa -C "[email protected]"
# 2. Add SSH public keys to the remote repository
# 3. Associate the remote library
git remote add origin git@server-name:path/repo-name.git
# 4. Push all the contents of the Master branch for the first time. After that, just use Git push Origin Master to push the latest changes
git push -u origin master
Copy the code

10. Collaborate in groups

# 1. View the remote storage details
git remote -v
# 2. Push local branch to remote repository,
  #1. If there is a conflict, try merging with git pull. If there is a conflict, resolve the conflict and push again
  #2 if no tracking information is displayed after git pull, proceed to the fourth item, establish the association, and then pull, and then merge if there is conflictGit push Origin local branch name# 3. Create a local branch that corresponds to the remote branchGit checkout -b origin/ remote branch name# 4. Establish association between local branch and remote branchGit branch --set-upstream name of origin/ remote branchCopy the code

11. Branch policy

1. The master branch should be very stable, that is, only for new releases, no work should be done on it; 2. Do all the work on the dev branch, that is, the dev branch is unstable, at some point, such as 1.0 release, then merge the dev branch to master, and release 1.0 on master branch; 3. Everyone has their own branch, just merge on dev branch from time to timeCopy the code

supplement

# Git fetch vs. git pullCompared with Git pull, Git fetch is equivalent to fetching the latest version from remote to local, but does not automatically merge. If you need a selective merge git fetch is a better choice. Git pull is faster with the same effect.View the current state of the repositoryGit status // Check the git commit loglog

You can use HEAD to indicate the current version,HEAD^ to indicate the previous version,HEAD^^ to indicate the previous versionGit reset --hard HEAD^ or git reset --hard 3628164logCommit = commit = commit = commit = commitGit reflog is used to record every command you run.
git reflog

Workspace and staging areaGit's repository contains many things. The most important of these is the staging area called stage (or index), the first branch that Git automatically creates for us, the master, and a pointer to the master called HEAD. Using Git Add to add a file is essentially adding file changes to the staging area; Committing your changes with Git Commit is essentially committing the entire contents of the staging area to the current branch# Undo changes1. Discard the workspace changes directly by running git checkout -- file 2. The changes have been added to the staging area. If you want to discard the changes, do two steps. The first step is to run the git reset HEAD file command to return to scenario 1, and the second step is to follow scenario 1Copy the code

Finally, welcome everyone to pay attention to the public account of Interesting Talk, join the front-end technology group, and discuss the charm of the front-end together. The Vue-React-Miniprogram-Node communication and learning group we established has been joined by hundreds of front-end partners of various enterprises.

More recommended