Repository
Let's first get to know the repository - Repository
all the basic commands we mention next Git
are based on the repository.
So what is a repository? The version library is also known as the warehouse, and its English name repository
can be simply understood as a directory. All files in this directory can be Git
managed, and the modification and deletion of each file Git
can 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 Git
the related commands for repository creation.
Initialize the local repository - git init
-
Create an empty directory as the project folder
//创建一个空目录 mkdir ios-git cd ios-git pwd <!-- /Users/xxx/ios-git -->
-
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 directoryempty Git repository
under 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..git
Git
.git
ls -ah
-
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 clone
At this time , the command will be used Git
to clone Git
almost 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-docs
and initialize a folder in this directory .git
. If you enter this newly created help-docs
folder, 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 -s short 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 -a representative of your submission is add , that is, you don't need to use add the command separately. |
Pull | git pull git pull origin git pull origin master:dev |
git pull origin master:dev Pull master branch and merge into dev branch |
to push | git push git push origin master git push --force origin master force push |
If there is a difference between the local version and the remote version, but you want to force push, you can use --force the parameter |
View log | git log git log --oneline git log --reverse --oneline |
View the current HEAD and previously submitted history records. --oneline Concise --reverse output. 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 HEAD represents 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~100 It can also be reset to the specified submission record that commit_id reset will HEAD point to its own reset commit_id , and commit_id subsequent submission records will be cease to exist |
Restore/Rollback | git revert --hard HEAD^ git revert --hard commit_id |
git revert It is used to redo the content of a certain commit submission. A new commit submission will be created on the branch. When we restore commit_id this 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 HEAD the history of commits before and after the current one (that is, the entire branch). If you reset regret it the next day and git log can’t find your commit record yesterday, git reflog just use it and git reset --hard commit_id go back to the original code, reflog which is not Git the warehouse . Part of it, which is a file locally .git/logs/HEAD or in .git/logs/refs a directory, you can only see the commit records you operate locally. |
Supplementary submission | git commit --amend git commit --amend -m "提交描述" |
When you find that there are git commit still git push modifications that need to be submitted, but you don't want to add more commit records , git commit --amend a new record will be created to commit_id overwrite the previous record commit_old . git log When you view the record, you will see the new commit_id record, and the previous commit_old record will be gone. |
Unstage | git reset HEAD git 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 branch View local branch git branch -a View 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.0 git tag -a v1.0 -m "发布1.0版本" |
|
View all tags | git tag View local labels git ls-remote --tags origin View remote labels |
|
Delete tag | git tag -d v0.1 Delete local label git push origin :refs/tags/v0.1 Delete remote label |
If you have already pushed to the remote, you must use these two commands. |
push tag tag | git push origin v0.1 Push a single taggit push --tags git push origin --tags |
git push --tags It 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 stash will only operate on the git tracked files. Newly added files will not be entered into the cache because git the 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 stash is 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.git git stash@{$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 |