Git: Use in actual development (enough for you to use for ten years)

Repository

Let's first get to know the repository - Repositoryall the basic commands we mention next Gitare based on the repository.

So what is a repository? The version library is also known as the warehouse, and its English name repositorycan be simply understood as a directory. All files in this directory can be Gitmanaged, and the modification and deletion of each file Gitcan be tracked, so that the history can be tracked at any time, or Can be "restored" at some point in the future.

Warehouses are divided into local warehouses and remote warehouses:

  • Local warehouse : the history of the project stored on your local development machine (computer)
  • Remote warehouse : the history of the project on the server (such as GitHub, GitLab)

Next, let’s first look at Gitthe related commands for repository creation.

Initialize the local repository - git init

  1. Create an empty directory as the project folder

    //创建一个空目录
    mkdir ios-git  
    cd ios-git  
    pwd
    <!-- /Users/xxx/ios-git  -->
    
  2. Initialize warehouse

    git init
    

    The local warehouse is built in an instant Git, and it tells you that it is an empty warehouse ( ). At the same time, there is an additional directory empty Git repositoryunder the current directory . This directory is used to track and manage the version library. If you do not see the directory, it is Because this directory is hidden by default, you can see it with the command..gitGit.gitls -ah

  3. Connecting to the remote warehouse
    So far, the warehouse has only been created locally. In order for everyone to access it during team collaboration, the code needs to be placed in the remote warehouse (you can create a remote warehouse on GitHub, Gitee or GitLab):

git remote add origin ssh://[email protected]:2289/ios/docs.git
git add .
git commit -m "Initial commit"
git push -u origin main

This is how a complete warehouse is created.

Clone an existing repository - git clone

If a project already exists in the remote warehouse and you want to pull it down, that's easy. git cloneAt this time , the command will be used Gitto clone Gitalmost all the data on the warehouse server. Each version of each file will be pulled down.

git clone https://gitcode.net/codechina/help-docs

This will create a directory named in the current directory help-docsand initialize a folder in this directory .git. If you enter this newly created help-docsfolder, you will find that all the project files are already in it, ready and waiting for subsequent steps. development and use.

Customize local warehouse name

You want to customize the name of the local repository when cloning the remote repository:

git clone https://gitcode.net/codechina/help-docs mydocs

The above is the preparation work for development. After initializing Git, it is used at work. Here are some commonly used Git methods:

Git common methods

effect method Note description
Check the current status changes of the warehouse git status git status -sshort output Check whether there are any uncommitted files in the workspace and staging area
Comparative changes git diff readme.txt Check the specific modified content of readme.txt
Add to git add readme.txt git add . Adding files from the workspace to the staging area git add .adds all modified files
submit git commit -m "提交备注" git commit -a git commit -am "提交备注" Submit all the contents of the staging area to the current branch. You can note in "" what the -arepresentative of your submission is add, that is, you don't need to use addthe command separately.
Pull git pull git pull origin git pull origin master:dev git pull origin master:devPull masterbranch and merge into devbranch
to push git push git push origin master git push --force origin masterforce push If there is a difference between the local version and the remote version, but you want to force push, you can use --forcethe parameter
View log git loggit log --oneline git log --reverse --oneline View the current HEADand previously submitted history records. --onelineConcise --reverseoutput. Output in forward order. By default, the most recent time is output in reverse order.
View modification history git blame readme.txt View the modification record of the readme.txt file
reset/rollback git reset --hard HEAD^ git reset --hard commit_id In Git, use HEADrepresents the current version, which is the latest submission e55063a. The previous version is HEAD^, the previous version is HEAD^^, 100 is ^written as 100. HEAD~100It can also be reset to the specified submission record that commit_id resetwill HEADpoint to its own reset commit_id, and commit_idsubsequent submission records will be cease to exist
Restore/Rollback git revert --hard HEAD^ git revert --hard commit_id git revertIt is used to redo the content of a certain commitsubmission. A new commitsubmission will be created on the branch. When we restore commit_idthis record, the code submitted by this record will be cleared. The code before and after this record is still there.
View local logs git reflog View HEADthe history of commits before and after the current one (that is, the entire branch). If you resetregret it the next day and git logcan’t find your commit record yesterday, git reflogjust use it and git reset --hard commit_idgo back to the original code, reflogwhich is not Gitthe warehouse . Part of it, which is a file locally .git/logs/HEADor in .git/logs/refsa directory, you can only see the commit records you operate locally.
Supplementary submission git commit --amendgit commit --amend -m "提交描述" When you find that there are git commitstill git pushmodifications that need to be submitted, but you don't want to add more commitrecords , git commit --amenda new record will be created to commit_idoverwrite the previous record commit_old. git logWhen you view the record, you will see the new commit_idrecord, and the previous commit_oldrecord will be gone.
Unstage git reset HEADgit reset HEAD readme.txt To restore files from the staging area to the workspace, the reset command mentioned above is added --hard, which means that the workspace and staging area are restored to their original state.
Undo edits git checkout . git checkout -- readme.txt When the file is not placed in the temporary storage area in the workspace (not added), you want to give up modifications to the file git checkout .and give up everything.
View branches git branchView local branch git branch -aView remote branch
Create a branch git checkout -b dev git branch dev Create dev branch
switch branch git checkout dev git switch dev Cut to the dev branch
Merge the specified branch into the current branch git merge dev Merge dev branch into current branch
delete branch git branch -d dev
Create tags git tag v1.0git tag -a v1.0 -m "发布1.0版本"
View all tags git tagView local labels git ls-remote --tags originView remote labels
Delete tag git tag -d v0.1Delete local label git push origin :refs/tags/v0.1Delete remote label If you have already pushed to the remote, you must use these two commands.
push tag tag git push origin v0.1Push a single taggit push --tagsgit push origin --tags git push --tagsIt is enough to use it directly during development
View cache list git stash list
cache git stash git stash save git stash save "xxx" Removing files from the workspace and placing them in the cache stashwill only operate on the gittracked files. Newly added files will not be entered into the cache because gitthe new files have not been tracked yet.
Remove cache git stash pop git stash pop stash@{$num} Take out the cache to the workspace and remove it from the stack . The latest stash@{num} in the stack stashis restored by default . It is recommended to use it when there is only one item in the stack to avoid confusion. It can also be specified.gitgitstash@{$num}
restore cache git stash apply git stash apply stash@{$num} Restore the contents of the stack to the current branch. This command will not delete the cache from the stack.
Remove cache git stash drop git stash drop stash@{$num} Remove from the stack stash, remove the specifiedstash@{$num}
Empty the cache git stash clear Remove allstash

Guess you like

Origin blog.csdn.net/wujakf/article/details/128532059