Git manages the use of files and code
Git is a free, open source, distributed version control system designed to handle everything from small to large projects quickly and efficiently. Git is easy to learn, has a small footprint, and has lightning-fast performance. It has SCM tools like Subversion, CVS, Perforce, and ClearCase, and features such as inexpensive local branches, convenient staging areas, and multiple workflows. — Git website
I still remember when I wrote my thesis, I saved my thesis document like this, and my code folder was like this. According to demand to delete or modify some things after save as another file, after a period of time, want to find the deleted text/code, but don’t know which is saved in the file, wants to keep the latest one deleted all the other, again afraid one day will use can’t delete, really annoying, this time is our strong Git turn appeared, By the way, I would like to record my learning from the Git tutorial of Liao Xuefeng’s official website.
Commands I use in my work
1. If you do not have the project code, the project leader will give you a link, at this time you can use the git command git clone
git clone https://github.com/CodingEmbedded/mygit.git
2. Generally speaking, I need to build a new branch to develop the supervisor’s demand (I want to get my ZFB payment code at the moment)
Git check -b git check -b
3, at this time can be happy to write about the bug, sometimes colleagues/supervisor asked you to change what problem, you changed a lot can not remember first
diffSo you can see that we changed those files and added and deleted those things
4, you can write after the first
git commitCommit native code
git commit -m "message"
5. Push is required to submit to the remote repository
6. If someone submits code while you’re writing, you need to pull the latest code down
If there is no conflict, then commit the code (if there is a conflict, follow Git instructions to make changes before committing).
Commit_id = commit_id = commit_id = commit_id = commit_id = commit_id = commit_id = commit_id = commit_id = commit_id
Then go back to version:
git reset --hard commit_id
Finally force push to remote branch:
git push -f
It can be seen from Figure 1 that a record is committed as “add or del”, Figure 2 and Figure 3 are backtracking version operations, and Figure 4 and Figure 5 show that Git has backlogged
Create a repository
1. Create an empty directory (I am E-drive – > code directory create a test library)
2. Use the git init command to turn this directory into a repository that git can manage
ls -ahTo view
.git> directory and
../>, that’s because
.gitThis directory is hidden by default.
Add files to the warehouse
1. Write a file text.txt
2, Use git add
command to add multiple files, add all files using git add-a
Use the command git commit-m
3. Check the working status and modified files
1. To keep track of the status of the workspace, use git status command.
If git status tells you that a file has been modified, you can use git diff to see the changes.
Git version rollback
You can use Git Log to review your commit history and determine which version you want to backtrack to.
2. The version HEAD points to is the current version, so Git allows you to go back between versions and use commands
Git reset –hard commit_id
Git must know the current version of the Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git
4. To go back to the future, use git reflog to look at the command history to determine which version to go back to. Use the command git reset –hard commit_id.
V. Temporary storage area
The process of submitting a README.txt
VI. Management modification
If you do not add Git to the staging area, you will not be added to the commit.
7. Revocation of amendments
Scenario 1: When you change the contents of a file in your workspace and want to discard the workspace changes, use the git checkout — file command.
Scenario 2: If you have changed the contents of a file in your workspace and added it to the staging area, then you want to discard the changes. The first step is to command git reset HEAD
to return to Scenario 1. The second step is to follow Scenario 1.
Scenario 3: If you have already committed an inappropriate change to a repository and want to undo the commit, refer to Git’s version rollback section 4 (see the git log for command history to determine which future version to go back to). Git reset –hard commit_id) if it is not pushed to a remote repository.
8. Delete files
The git rm command is used to delete a file. If a file has already been committed to a repository, then you never have to worry about accidentally deleting it, but be careful, you can only restore the file to the latest version, and you will lose * what you changed * since the last commit.
Git checkout –
> git checkout –
> git checkout –
> git checkout –
> git checkout –
> git checkout –
Add a remote library
Step 1: Create an SSH Key under Git. In the user’s home directory, see if there is an.ssh directory, and if so, see if there are id_rsa and id_rsa.pub files in that directory. If there are, skip to the next step. If not, open a Shell (open Git Bash on Windows) and create an SSH Key:
$ssh-keygen -t rsa -C "[email protected]"
You need to change the email address to your own email address, then press Enter all the way to the default value. Since this Key is not for military purposes, there is no need to set a password.
If all goes well, you can find the.ssh directory in the user’s home directory, which contains two files, id_rsa and id_rsa.pub, which are the secret Key pairs for the SSH Key. ID_rsa is a private Key that cannot be disclosed, and id_rsa.pub is a public Key that you can tell anyone with confidence.
Step 2: Log in to GitHub, open “Settings”, click “SSH and GPG Keys”, fill in any Title, paste the contents of the id_rsa.pub file in the Key text box, click “Add Key” and you should see the added Key:
Step 2: Create a GitHub account and add an empty repository. First, log in to GitHub. Then, find the “New Repository” button in the upper right corner to create a new repository: To Create a Repository, click the “Create Repository” button and you will successfully Create a new Repository:
Currently, the repository on GitHub is empty, and GitHub tells us that we can clone a new repository from it, or associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository.
Now, let’s follow the prompts from GitHub and run the command: git remote add origin + SSH link from our local repository
Use the command git push-u origin master to push the content.
Note: Check that the Git configuration file’s mailbox and username and URL are correct. Before we can proceed to the next step.
To associate a remote library, use the command git remote add origin git@server-name:path/repo-name.git; After the association, use the command git push-u origin master to push all the contents of the master branch for the first time; Thereafter, after each local commit, you can use the command git push origin master to push the latest changes whenever necessary
Cloning from a remote library
To clone a repository, you must first know the repository’s address and then clone it using the git clone+ SSH link command.
Git supports a variety of protocols, including HTTPS, but the native Git protocol supported via SSH is the fastest.
Creating and Branching (Git encourages a lot of branching)
See the branch: git branch
Switch branch: git checkout
Git checkout -b
Git branch -d
When Git cannot automatically merge branches, you must resolve conflicts first. After resolving the conflict, submit again and the merge is complete. Resolving the conflict is to manually edit the failed Git merge file into the desired content and then commit it. Use the git log –graph command to see the branch-merge graph.
XIII. Branch Management Strategy
Mysql > merge the dev branch to create a new commit; mysql > merge the dev branch to create a new commit; merge the dev branch to create a new commit; Write in the COMMIT description.
14. Bug branch
When fixing a bug, we fix it by creating a new bug branch, then merging, and finally deleting. When you don’t get your work done, do a git stash in the field, fix the bug, and then go back to the field with the git stash pop.
15. Feature branch
To develop a new feature, it is better to create a branch. Git branch -d
If you want to drop a branch that has not been merged, you can use git branch -d
to delete it.
16. Collaborate with multiple people
To view remote library information, use git remote-v;
Locally created branches are not visible to others if they are not pushed to a remote location.
Push the branch locally and use git push origin branch-name. If push fails, use git pull to grab the new commit remotely.
Git checkout -b branch-name origin/branch-name; git checkout -b branch-name origin/branch-name;
Git branch –set-upstream branch name origin/branch name; git branch –set-upstream branch name origin/branch name
Grab the branch remotely and use Git Pull. If there is a conflict, handle the conflict first.
Create a tag
The git tag
command is used to create a new tag.
Command git tag-a
-m “blablabla…” Label information can be specified;
The command git tag allows you to see all the tags
You can see the instructions with the command git show
Note that the tag is always linked to a commit. If the Commit appears on both the master branch and the dev branch, the tag will be visible on both branches.
18. Operate labels
Git push origin
to push a local tag;
Git push origin –tags can push all unpushed local tags;
The command git tag-d
removes a local tag;
The command git push origin :refs/tags/
removes a remote tag. To summarize the common Git commands:
mkdirXX (create an empty directory XX refers to the directory name)
pwdDisplays the path to the current directory.
git initTurn your current directory into a manageable Git repository and generate hidden.git files.
git add XXAdd the XX file to the staging area.
Git commit -m "XX"The commit file XX is a comment for the commit.
git statusView warehouse status
git diff XXLook at the XX file to modify those contents
git logView History
git reset --hard HEAD^or
git reset --hard HEAD~
Git reset — hard commit_id (commit_id)
cat XXView the content of XX file
git reflogView the version number ID of the history
git checkout -- XXUndo all changes to the XX file in the workspace.
git rm XXDelete XX file
Git remote add origin + SSH linkAssociate a remote library
The git push - u(No need after the first use of -u)
origin masteR pushes the current master branch to the remote library
Git clone + SSH linkCloning from a remote library
Git checkout - b devCreate the dev branch and switch to the dev branch
git branchView all current branches
git checkout masterLet’s go back to the master branch
git merge devMerge the dev branch on the current branch
Git branch - d devDelete dev branch
git branch nameCreate a branch
git fecthManually pull remote warehouse updates
git stashHide your current work until you are back on site
git stash listView a list of all hidden files
git stash applyRecovering hidden files, but not deleting the contents
git stash dropDelete the file
git stash popRecovering the file also deletes the file
git remoteView remote library information
Git remote - vView details of remote libraries
git push -u origin/masterGit will force a commit to
git --amendIn simple terms, this can be understood as making corrections to the last commit. < if the current last commit did not
- Git tutorial – Liao Xuefeng’s official website https://www.liaoxuefeng.com/wiki/896043488029600
If you think the article is good, remember to “thumb up follow”.
Follow my WeChat official account [Work overtime apeYou can get more content