Skip to content

Etc git commands

This blog talks about the git commands for the changes between untracked, modified and staged. Moreover, it introduces the git remote operation and shallow clone.

Untracked, Unmodified, Modified and Staged

Git document shows a flow for the untracked, modified and staged. It focus on the changes before commiting.

  • Untracked: git doesn't know the existence of a file.
  • Unmodified: git knows this file and knows it hasn't been modified.
  • Modified: git knows this file and it has been changed.
  • Staged: the changes has been added into git, which could be committed

  • git add: add untracked/modified files to staged

  • git clean: remove untracked file
  • git checkout -- .: remove modified changes, it doesn't apply on the staged changes
  • git reset: move staged changes into modified state
  • git restore --staged: ditto
  • git rm --cache: ditto
  • git reset --hard: remove staged/modified/untracked changes
  • git checkout --: remove modified changes
  • git restore: ditto
  • git commit: commit staged changes
  • git commit -a: commit modified files without moving them into staged state first

Git Remote Add

Generally, there is an origin whne you git clone a repo. However, a git repo could be set up with multiple repo.

For example, we don't clone a repo, instead we use git init to retrieve it.

git init example && cd exmaple
git remote add origin git@github.com:xieyuschen/example
After setting up, if you pull directly, it will reports an error:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>
Hence, you have two ways, they work differently.
# temporary solution, because every time you need to specify the branch when you pull
git pull origin master
# connect your current branch to the remote origin
git branch --set-upstream-to=origin/master
If you connect the local branch to remote, there will be a configuration in .git/config:
[branch "master"]
    remote = origin
    merge = refs/heads/master
Hence, through this way, git connects the current branch to the remote repo.

Moreover, git allows you to add multiple remote source for one git repo, run git remote add <name> <remote> could achieve it.

Git Shallow Clone

I saw github action checkout allows you to specify depth to retrieve limited commit history instead of the whole git history.

git init example && cd exmaple
git remote add origin git@github.com:xieyuschen/example
git -c protocol.version=2 fetch --prune --progress --no-recurse-submodules --depth=1 origin
git checkout --progress --force -B master refs/remotes/origin/master
After this, it will only exist one commit because of --depth=1.

Moreover, we could archive a repo:

mkdir archieve_ && cd $_
git archive --remote=ssh://git@github.com/xieyuschen/example--format zip --output archive master
unzip archive

Then, we git clone directly and name it as cloned, you can see there is a different between

➜  trygit ✗ du -hs cloned    
4.8M    cloned
➜  trygit ✗ du -hs example
4.1M    example
➜  trygit ✗ du -hs archive_  
1.9M    archive_

Can read this docs to learn more about the git speeds up.