Git CLI hints

From WikiMLT
Revision as of 12:51, 3 January 2023 by Spas (talk | contribs) (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:DevOps and SRE)

Ap­pend a file to a branch

touch file.name
git add -A 
git commit -am "my update msg" 
git push

How to shrink the .git fold­er

git gc --aggressive --prune                # will perform garbage collection in your repository and prune old objects.
git repack -a -d --depth=250 --window=250  # It's better to use this command, because 'git gc --aggressive' is considered to be bad practice.

Delete all com­mit his­to­ry

git checkout --orphan latest_branch     # Checkout
git add -A                              # Add all the files to the 'current' branch
git commit -am "commit message"         # Commit the changes
git branch -D main                      # Delete the branch 'main'
git branch -m main                      # Rename/move the 'current' branch to main
git push -f origin main                 # Finally, force update your repository

Delete/​​​Remove a branch

git branch -d <branch-name>

Re­move last Com­mit and Push from the Re­mote

git reset --soft HEAD~1
git push -f origin HEAD^:BRANCH_NAME

Now you are ready to push the same changes by a new com­mit.

Copy File from An­oth­er Branch

git restore --source master ./assets/css/src/style.less

Where mas­ter is the name of the branch we want to copy from.

Hard Re­set a Lo­cal Branch

BRANCH="master"
git stash
git fetch --all
git reset --hard FETCH_HEAD
git clean -df        
git checkout "$BRANCH"
git pull --all

Push a New Lo­cal Branch to a Re­mote Git Repos­i­to­ry and Track It

BRANCH="Stage-5"
git checkout -b "$BRANCH"
git push -u origin "$BRANCH"

Ref­er­ences