Git combat articles
Help you get started with git quickly through examples, easy to understand! (The example environment of this practical article uses Windows git bash)
Install
Create new warehouse
Order:git init
Select a folder, open it and execute it git init
to create a new git
warehouse.
In this way, you can see a .git file under the folder
If you can't see it, don't panic, it's just that the hidden file is not turned on, you can see it by turning on the option to make the hidden file visible.
checkout warehouse
In the real development environment, we need to clone ( clone
) the remote warehouse to the local.
Order:git clone
git clone username@host:/path/to/repository
Here we come to git clone
a remote warehouse:
git clone https://github.com/rookies-of-XHS/git-application-docs.git
The screenshot after success is as follows (if it fails, it may be a problem such as the network, please try patiently or check whether the clone link is wrong)
Now we will clone the remote to the local, you will find a folder appears, we enter the folder.
add and submit
Content addition: Add the files to be uploaded or modified to the temporary storage area.
Order:git add <filename>
git add *
Let's add some content to the folder first, and I will put a document in it.
We have added this document to the temporary storage area, and the next step is to submit the contents of the temporary storage area.
Content submission: organize, confirm and submit the files in the cache area.
Order:git commit
git commit -m "代码提交信息"
Let's submit a content information here:
git commit -m "docs: 加入git实战篇文档"
change push
Push: Push the local changes to the remote warehouse
Order:git push
git push origin master
You can change the master to any branch you want to push (we push to the main branch by default)
git push
the branch
Branches are used to insulate feature development. When you create a repository, master is the "default" branch. Do development on other branches and merge them into the main branch when you're done.
Create a branch and switch:git checkout -b feature_x
Switch back to the master branch:git checkout master
Delete the newly created branch:git branch -d feature_x
Use this branch to push:git push origin <branch>
Unless you push the branch to the remote repository, the branch is invisible to others
update and merge
Update: pull the remote update to the local
Order:git pull
Merge: to fetch (fetch) and merge (merge) remote changes in your working directory . To merge other branches into your current branch (e.g. master)
Order:git merge <branch>
Note: In both cases, git will try to automatically merge the changes. Unfortunately, this may not be successful every time and conflicts may arise . At this time, you need to modify these files to manually merge these conflicts .
After making changes, you need to execute the following command to mark them as merged successfully:
git add <filename>
Before merging the changes, you can preview the differences with the following command:
git diff <source_branch> <target_branch>
view history
Order:git log
This is the easiest way to view history
Of course, View History has various parameters to help you view the results you need, and you can use git log --help
View All Parameters.
Replace local changes
If you make a mistake (of course, this should never happen), you can replace the local changes with the following command:
git checkout -- <filename>
This command will replace the files in your working directory with the latest contents from HEAD. Changes already added to the staging area, as well as new files, will not be affected.
If you want to discard all your local changes and commits, you can go to the server to get the latest version history and point your local master branch to it:
git fetch origin
git reset --hard origin/master
Note: Operations such as rolling back and discarding must be used with caution, as it may cause problems such as file loss.