Git CLI hints: Difference between revisions

From WikiMLT
Line 35: Line 35:
git push -f origin HEAD^:master
git push -f origin HEAD^:master
</syntaxhighlight>Now you are ready to push the same changes by a new commit.
</syntaxhighlight>Now you are ready to push the same changes by a new commit.
== Copy File from Another Branch ==
<syntaxhighlight lang="shell" line="1">
git restore --source master ./assets/css/src/style.less
</syntaxhighlight>


== Hard Reset a Local Branch ==
== Hard Reset a Local Branch ==
Line 55: Line 60:
* DevConnected: [https://devconnected.com/how-to-undo-last-git-commit/ How To Undo Last Git Commit]
* DevConnected: [https://devconnected.com/how-to-undo-last-git-commit/ How To Undo Last Git Commit]
* Stack Overflow: [https://stackoverflow.com/a/449070/6543935 How can I remove a commit on GitHub?]
* Stack Overflow: [https://stackoverflow.com/a/449070/6543935 How can I remove a commit on GitHub?]
* Stack Overflow: [https://stackoverflow.com/a/60855504/6543935 How do I copy a version of a single file from one Git branch to another?]
<noinclude>
<noinclude>
<div id='devStage'>
<div id='devStage'>

Revision as of 12:14, 14 November 2022

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^:master

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

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

Ref­er­ences