Some problems about workspace, staging area, local warehouse and remote warehouse in git

principle

Refer to Ruan Yifeng's blog: List of common Git commands

  • Workspace: the code written directly and stored on the hard disk in the form of a file is actually saved in the workspace workspace;
  • index/stage: git add will be stored in the stage/index temporary storage area;
  • Repository: after git commit, it will be stored in the local repository;
  • remote: git push will be stored in remote warehouse

If you still don't understand, you can continue to refer to:

1. gitignore and delete the directory to be ignored from the remote git

reference resources: idea creation gitignore and delete the directory to be ignored (such as. idea) from the remote git

Problem Description:

  • Contents in git of remote:
  • Contents in. gitignore:
    d2l_zh_jupyter/self_exercise/.ipynb_checkpoints/
    
  • Contents of local git warehouse:

As you can see,

  • Although it's mine The gitignore file contains this folder, but the git warehouse of remote still has this folder and has not been deleted. But its file content is really different from that of the local.
  • This is because I created it after uploading it for some time gitignore file, so there are some problems.
# Stop tracking the specified file, but the file will remain in the workspace (if it is a folder, it needs to be removed iteratively, and the parameter - R needs to be added)
git rm --cached -r d2l_zh_jupyter/self_exercise/.ipynb_checkpoints/
git add .gitignore
git commit -m "gitignore Submit delete ipynb_checkpoints" 
git push origin master

After performing the above operations, although the files in the workspace workspace still exist, they will not be tracked (ignored), and the remote side folder will disappear after uploading

2. An error is reported when uploading a file exceeding 100MB


reference resources: Handle the problem that GitHub is not allowed to upload files larger than 100M

When github uploads a file, if it exceeds 50M, it will be warned, and if it exceeds 100M, it will be rejected directly

If you still want to upload this large file, you can refer to: git large file storage , this is another tool that git officials have specially made for large files

3. Your branch is ahead of 'origin/master' by N commits

reference resources: git large file storage , because the large file exists, you need to delete the large file from the last commit before you can continue to execute push

# First delete the non-conforming file from the temporary storage area
 git rm --cached /Users/Dora/Desktop/XXX/XXX/my.txt
# After this step, the large file will be removed from your commit record, and then you can push the local code to github
 git commit --amend -CHEAD

BUT. . . Because I did not operate in the normal way during the upload process, two incomplete commitments are displayed. You can use git status and git log to view the uploaded branches, etc

$ git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        "4. \351\253\230\347\255\211\346\225\260\345\255\246 \347\254\2547\347\211\210 \344\270\213\345\206\214 \345\220\214\346\265\216\345\244\247\345\255\246.pdf"

no changes added to commit (use "git add" and/or "git commit -a")

reference resources:

Combined with this part of the figure.

  • Workspace: the code written directly and stored on the hard disk in the form of a file is actually saved in the workspace workspace;
  • index/stage: git add will be stored in the stage/index temporary storage area;
  • Repository: after git commit, it will be stored in the local repository;
  • remote: git push will be stored in remote warehouse

Simply put, this prompt message appears because you made some changes to the local master (warehouse), but did not send it to the remote end. Choose different processing methods according to your own remote and local warehouses:

  1. In a good workflow, the master copy of the remote warehouse should be intact, and the master stored locally should only be a copy of the remote warehouse (the contents of the remote warehouse and the local warehouse should be the same...), This prompt does not appear in this workflow.
  2. If another workflow changes locally, git push origin directly (assuming origin is your remote end)
  3. If the local change is wrong, remove it and reset the local master to the same state as the remote. Use git reset --hard origin/master

Take my repo as an example, run it in the local git warehouse:

$ git log
commit e7dad275beef801b602655f60fba68451ae03f05 (HEAD -> main)
Author: huangs <XXX@XXXX.com>
Date:   Sun Jan 2 11:10:15 2022 +0800
    First upload
commit 88e010f7784bdce51e049de44ee34a60db306794
Author: huangs <XXX@XXX.com>
Date:   Sun Jan 2 00:23:41 2022 +0800
    First upload
# This was brought from the github clone. (it was filled out when the project was created on github because README and. gitignore files were added)
commit 247a7060f4f2a0b209d2deba07411b36adf5035e (origin/main, origin/HEAD)
Author: CastleDream <35064479+CastleDream@users.noreply.github.com>
Date:   Sat Jan 1 16:05:03 2022 +0800
    Initial commit

$ git branch -v
* main e7dad27 [ahead 2] First upload

You can see that the original remote content is actually: origin/HEAD, so the command I use should be

!!!!!!!
Note that after executing this command, the changed content after this node will disappear! (just like system image)

$ git reset --hard origin/HEAD
HEAD is now at 247a706 Initial commit

If you find that the files you need are not backed up, you can restore the contents of the latest commit
The following e7dad27 comes from the commit e7dad275beef801b602655f60fba68451ae03f05 of git log (generally the first seven digits are OK)

$ git reset --hard e7dad27
Updating files: 100% (17/17), done.
HEAD is now at e7dad27 First upload

Then it will recover. reference resources: Undo git reset --hard HEAD~1

However, it seems that if you don't want this your branch is ahead of 'origin / Master' by 3 commitments in git status, you can only reset to the original state

$ git reset --hard origin/HEAD
HEAD is now at 247a706 Initial commit
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Therefore, you can only back up the modified things yourself, then reset them, confirm that the status is OK, and then copy the contents in the backup. Re upload.

Keywords: git github

Added by CMC on Tue, 04 Jan 2022 22:41:59 +0200