[Git Basics] Commonly used git commands (3)

1. Version rollback

1.1 The case of no commit

If you haven't committed yet, you can roll back with the git checkoutor command.git restore

For example, if you want to undo changes to file1.txt, you can use the following command:

git checkout -- file1.txt

or use git restorethe command:

git restore file1.txt

If you want to undo changes to all files, you can use:

git checkout -- .

or:

git restore .

1.2 The situation that has been committed but not pushed

If you have committed but not pushed yet, you can use the git resetor git revertcommand to roll back.

Use git resetthe command to roll back to the previous commit:

git reset HEAD~1

This will move the HEAD pointer to the previous commit and put the changes back into the working directory. You can use the --soft, --mixedor --hardoptions to specify the fallback method. By default, the pattern git resetis used --mixed.

git revertCreate a new commit to undo the specified commit using :

git revert HEAD

This will create a new commit that undoes the most recent commit. You can also use a commit hash or other reference to specify which commits to undo.

1.3 The situation that has been pushed to the remote warehouse

If you have already pushed, the process of rolling back will involve the remote repository. First, you need to use git resetor git revertmake a local fallback, and then force push to the remote repository.

Use git resetrollback to the previous commit and force push:

git reset HEAD~1
git push -f origin <branch_name>

Use to git revertcreate a new commit to undo the specified commit, and push:

git revert HEAD
git push origin <branch_name>

It should be noted that forced push may cause other people to lose submissions in the remote warehouse, so it should be used with caution in team collaboration.

2. Delete files

2.1 Delete files from workspace

  • First, delete the files in the workspace. For example, at the command line, enter the following command:
    rm 文件名.txt
    
    Replace "filename.txt" with the name of the file you want to delete.
  • Then, add the delete operation to the staging area:
    git add 文件名.txt
    
    Likewise, replace "filename.txt" with the name of the file you want to delete.
  • Commit changes:
    git commit -m "删除了文件名.txt"
    
    Replace "removed filename.txt" with the appropriate commit message.

2.2 Use the git rm command to delete files

  • Use git rmthe command to delete the file. For example, enter the following command:

    git rm 文件名.txt
    

    Replace "filename.txt" with the name of the file you want to delete.

    This will delete the files from the workspace and staging area.

  • Commit changes:

    git commit -m "删除了文件名.txt"
    

    Replace "removed filename.txt" with the appropriate commit message.

Of the two methods, using the git rm command is simpler because it removes files directly from the workspace and staging area without manually executing the git add command. Note, after deleting a file, make sure to commit the changes so that the changes are updated in the version history.

2.3 Permanently delete files

Sometimes we may accidentally upload some useless files, especially our developers who use C and C++, accidentally upload a lot of compiled intermediate files, of course, this operation can be avoided through .gitignore. But sometimes a secret key file may be uploaded, such key things are generally kept confidential, and general developers should not be able to see such a secret key file. It has been committed and pushed to the remote warehouse, how to delete it? And it should not exist in the record, for example, we want to permanently delete secret.cert:

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch secret.cert' --prune-empty --tag-name-filter cat -- --all

git push origin master --force

-rYou can also delete the folder, just replace secret.cert with the folder, and add parameters after rminsert image description here

2.4 Steps to permanently delete files

  • Use git logto find the commit hash of the file to delete:

    git log --pretty=oneline -- 文件名.txt
    

    Replace "filename.txt" with the name of the file you want to delete. Find the earliest commit hash from the output.

  • git rebaseDelete the file interactively with the command :

    git rebase -i 提交哈希^
    

    Replace "commit hash" with the hash found in step 1. Note that a caret (^) is added after it to indicate the previous commit of the hash.

    This will open a text editor showing a list of commits to edit.

  • In a text editor, find the line of the file that you want to delete, pickchange the beginning of the line to edit, save and close the file.

  • You are now in the commit state where you want to delete the file. Remove files from this commit:

    git rm --cached 文件名.txt
    

    Replace "filename.txt" with the name of the file you want to delete.

  • Recommit the changes with git commit --amendthe command:

    git commit --amend
    

    This will recommit that change, and delete the file.

  • Use git rebase --continuecontinue to rewrite the remaining history:

    git rebase --continue
    
  • Finally, push the changes to the remote repository:

    git push --force
    

    Note that using --forcethe option will overwrite the remote repository's history. This may affect other team members, so make sure to communicate with your team members before doing this.

After the above steps, the file will be permanently deleted, and the Git history no longer contains records about the file. However, this operation has certain risks, please proceed with caution.

3. View the modification of the specified file

3.1 View all commits of the file

git log --oneline [filename]

Display all modification records of a file
insert image description here

3.2 View the modified content of all commits

git log -p [filename]

Show all commit changes
insert image description here

3.3 View the modification in a commit

git show [commit_id] [filename]

Display the modification of a file in a commit
insert image description here

3.4 View the differences of local modifications

git diff [filename]

Check what specific changes have been made to a file locally
insert image description here

3.5 Compare with a commit

git diff [commit_id] [filename]

Display all the differences with a commit, commit-id can be replaced by HEAD, such as HEAD~2
insert image description here

3.6 Differences between two commits

git diff [commit_id1] [commit_id2]

Show all differences between two commits
insert image description here

4. Synchronization between multiple clients

4.1 Localization method

If we expect colleagues to pull and push codes from each other, we can use the following method:

  1. On each participant's computer, find the folder for the local Git repository. In this folder, execute the following command to start a Git server:

    git daemon --reuseaddr --base-path=. --export-all --verbose
    

    This command will start a Git server, allowing other participants to access the local repository.

  2. Get the IP address of each participant and make sure they are on the same LAN or VPN environment.

  3. On each participant's computer, add the other participant's repository as a remote repository. For example, suppose the IP address of colleague A is 192.168.1.2, then on the computer of colleague B, execute the following command:

    git remote add colleagueA git://192.168.1.2/项目文件夹名称
    

    This adds colleague A's repo as a remote. Please replace the IP address and project folder name according to the actual situation.

  4. When you need to get code updates from other colleagues, use the following command:

    git fetch colleagueA 分支名称
    git merge colleagueA/分支名称
    

    These two commands will fetch updates from colleague A's repository and merge them into the current branch. Please replace the branch name according to the actual situation.

  5. When you need to push the code to other colleagues, first make sure that the other party has added your warehouse as a remote warehouse. Then, use the following command:

    git push colleagueA 分支名称
    

    This command will push your branch to colleague A's repository. Please replace the branch name according to the actual situation.

Note: This method requires each participant's computer to be running the Git service and be accessible to each other. If there are network problems, the synchronization may fail. Moreover, this method does not have a centralized server to manage the version history, and conflicts may arise during the collaboration process. Therefore, it is recommended to use a centralized Git server for synchronization in practical applications.

4.2 Centralized approach

It is also realized through a centralized Git server (such as GitHub, GitLab, etc.) as a transfer station. Here are some basic steps:

  1. Create a remote warehouse: First, you need to create a remote warehouse on a Git server. This could be GitHub, GitLab, Bitbucket, etc. After creating a remote warehouse, you will usually get a URL for accessing the warehouse.

  2. Adding a remote repository: In the local repository, you need to add the remote repository as a "remote". Enter the following command in the terminal:

    git remote add origin 远程仓库的URL
    

    Here "origin" is the alias of the remote repository, you can change it as needed.

  3. Push the content of the local warehouse to the remote warehouse: Use the following command to push the content of the local warehouse to the remote warehouse:

    git push -u origin master
    

    This will push the local "master" branch to the remote repository's "master" branch. If you are using another branch, replace "master" with the corresponding branch name.

  4. Other clients clone the remote repository: On other clients, you need to clone the remote repository first. Use the following command:

    git clone 远程仓库的URL
    

    This will clone the remote repository locally and automatically set the "origin" remote.

  5. Pulling updates: When other clients have made changes to the remote warehouse and pushed them, you can use the following command to pull the latest changes to the local:

    git pull origin master
    

    Likewise, if you are using another branch, replace "master" with the corresponding branch name.

Guess you like

Origin blog.csdn.net/weixin_52665939/article/details/130277450