Git CLI hints: Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разутвърден]; Категория:DevOps and SRE)
Line 101: Line 101:
{{devStage  
{{devStage  
  | Прндл  = DevOps and SRE
  | Прндл  = DevOps and SRE
  | Стадий = 6
  | Стадий = 3
  | Фаза  = Утвърждаване
  | Фаза  = Разработване
  | Статус = Утвърден
  | Статус = Разутвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | АвтПт  = Spas
  | АвтПт  = Spas
  | УтвПт  = {{REVISIONUSER}}
  | УтвПт  = Spas
  | ИдтДт  = 5.07.2022
  | ИдтДт  = 5.07.2022
  | РзбДт  = 3.01.2023
  | РзбДт  = {{Today}}
  | АвтДт  = 3.01.2023
  | АвтДт  = 3.01.2023
  | УтвДт  = {{Today}}
  | УтвДт  = 13.01.2023
  | ИдтРв  = [[Special:Permalink/27692|27692]]
  | ИдтРв  = [[Special:Permalink/27692|27692]]
  | РзбРв  = [[Special:Permalink/32196|32196]]
  | РзбРв  = {{REVISIONID}}
  | АвтРв  = [[Special:Permalink/32198|32198]]
  | АвтРв  =  
  | РзАРв  = [[Special:Permalink/32193|32193]]
  | РзАРв  = [[Special:Permalink/32198|32198]]
  | УтвРв  = {{REVISIONID}}
  | УтвРв  =  
  | РзУРв  = [[Special:Permalink/32195|32195]]
  | РзУРв  = [[Special:Permalink/32215|32215]]
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Revision as of 13:12, 3 March 2023

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="master"
git checkout -b "$BRANCH"
git push -u origin "$BRANCH"

Stash and Pop Changes

BRANCH="master"
git branch             # Check does the curent local repo is attached to a remote branch
git chechout "$BRANCH" # Try to attach to the target "$BRANCH"...

At this stage if Git re­turns some­thing like: "You lo­cal changes will be over­writ­ten… Please com­mit your changes or stash them be­fore you switch the branch­es."

Then you can check­out new branch, like it is shown in the above top­ic: git check­out ‑b "$BRANCH". Then check­out the $BRANCH again, or you can stash your changes, check­out the tar­get $BRANCH (with­out cre­at­ing a new one) and then pop the stash them af­ter the pull of the re­mote changes:

git stash               # Stash the changes
git status              # Check does everything is clean
git checkout "$BRANCH"  # Checkout the target "$BRANCH" again
git stash pop           # Pop the stashed changes
git status              # Check which files are modified

Af­ter that you can com­mit, merge and push your changes.

Ref­er­ences