1. Preparation phase: Configure SSH keys
-
Configure Git user information:
-
git config --global "123456"
-
git config --global "yyy123@"
-
git config --list
-
-
Generate SSH key:
ssh-keygen -t rsa -C "yy123@"
When asked for the save location, press Enter to accept the default location. Then enter your password twice.
-
Confirm the key location:
-
cd ~/.ssh
-
ls
-
cat id_rsa.pub
Copy the output public key and add it to yourGitLabThe SSH keys settings for the account.
-
2. Operation process
-
Cloning the repository:
This clons the repository and switches directly to the specified branch, with the corresponding replacement on it.git clone -b feature-branch
- Switch or create a branch
1. Switch branches:
cd your_project
git checkout <branch-name>2. Create a new branch and switch:
cd your_project Enter the project
git branch View the current branch
git branch <branch-name> Create branch
git checkout <branch-name> Switch branches (can be displayed on the terminal)
1. If you want to switch to another branch, you can use the following command: git checkout <branch-name>
2. If you want to create a new branch based on an existing branch and switch to the new branch immediately, you can use:
git checkout -b <new-branch-name> <existing-branch-name>
3. If you want to restore the file to the state of the last commit, you can execute: app_new.py
git checkout app_new.py
3. Update the code
Delete the old code and keep the .git directory:
Delete the code cloned above, leaving only.git
Table of contents. Delete all files and subdirectories, but keep.git
Table of contents. Then copy your project to that folder.
4. Add and submit changes
-
Add changes:
-
git add .
-
# Or add a specific file
-
git add your_file.py
-
-
Submit changes:
git commit -m "Add detailed description of what you changed"
5. Push changes to remote repository
-
Push changes:
git push origin <your-branch>
Create a merge request
On GitLab, create a merge request (Merge)Request) to merge your branch into the main branch.
About Listing Branches
-
List remote branches:
git branch -r
-
List all branches (including local and remote):
git branch -a
3. After you change the code, submitting changes to the Git repository usually involves the following steps:
Step 1: Check for changes
First, usegit status
The command checks for changes in your current working directory:
git status
This lists all changes that have not been staged, including new files, modified files and deleted files.
Step 2: Save changes
For changes you want to include in this submission, you need to usegit add
Command to temporarily save it. You can choose to stage all changes, or just stage specific files:
-
Save all changes:
git add .
This adds all modified files, new files and deleted files in the working directory to the temporary storage area.
-
Stories specific files:
git add path/to/your/file
Will
path/to/your/file
Replace with the file path you want to temporarily save.
Step 3: Submit changes
After temporary storage of changes, usegit commit
The command creates a new commit:
git commit -m "Your commit message"
Enter your submission information in double quotes to describe what changes you made. A good submission should be concise and clear, describing the purpose and content of the change.
Step 4: Push changes
After submitting the changes, usegit push
Command pushes your commit to the remote repository:
git push origin <branch-name>
Will<branch-name>
Replace with the name of your current branch.
Example
Suppose you modified a nametrain_net.py
The following is the complete submission and push process:
-
# Check for changes
-
git status
-
-
# Save changes
-
git add train_net.py
-
-
# Submit changes
-
git commit -m "modify train_net.py"
-
-
# Push changes to remote repository
-
git push origin Your branch
If you have modified multiple files, or added or deleted files, you can reuse themgit add
Command to save all relevant changes and submit them in one go.
4. If you want to delete certain files in the Git branch, you can follow the following steps:
Make sure you are already on the target branch, and the next step is to delete the file you want to remove. Suppose you want to delete the nameYou can follow the following steps for the file:
Step 1: Confirm the file exists
First, make sure that the file does exist in your working directory:
ls path/to/your/directory
Step 2: Delete the file
Delete files in the local working directory:
rm path/to/your/
Or inWindowsUsed in the system:
del path\to\your\example.txt
Step 3: Usegit rm
Order
Remove files from Git trace:
git rm path/to/your/
This will mark the file as deleted, ready to go fromVersion controlRemoved in .
Step 4: Submit changes
Now you can submit changes to delete the file:
git commit -m "Delete from Your branch"
Make sure you clearly state the changes you made in the commit message.
Step 5: Push changes to remote repository
Finally, push your changes to the remote repository:
git push origin Your branch
This will sync changes to your deleted files in Your branch branch to the remote repository.
Through the above steps, you can safely delete files in Your branch branch and submit changes to the repository. If the file has been deleted, but you still see it in the repository, it may be because you haven't committed and pushed those changes yet. Make sure to perform all steps, especiallygit rm
、git commit
andgit push。