Git CLI hints: Difference between revisions
mNo edit summary |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<noinclude><!--[[Category:DevOps_and_SRE|?]]-->{{ContentArticleHeader/DevOps_and_SRE}}</noinclude> | <noinclude><!--[[Category:DevOps_and_SRE|?]]-->{{ContentArticleHeader/DevOps_and_SRE}}</noinclude> | ||
== Append a file to a branch == | == Git Config == | ||
The following command uses the <code class="noTypo">--global</code> flag and will be applied to the file <code>~/.gitconfig</code>. | |||
<syntaxhighlight lang="shell" line="1"> | |||
git config --global user.name "Spas Z. Spasov" | |||
git config --global user.email "spas.z.spasov@example.com" | |||
git config --global init.defaultBranch "master" | |||
git config --global remote.origin.prune true | |||
</syntaxhighlight> | |||
To apply an option to the repository's <code>.git/config</code>, you need to remove the <code class="noTypo">--global</code> flag. | |||
<syntaxhighlight lang="shell" line="1" class="mlw-shell-gray"> | |||
git config user.email "spas.z.spasov@work.com" | |||
git config remote.origin.prune false | |||
</syntaxhighlight> | |||
==Append a file to a branch== | |||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
touch file.name | touch file.name | ||
Line 15: | Line 29: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Delete all commit history == | ==Delete all commit history == | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
git checkout --orphan latest_branch # Checkout | git checkout --orphan latest_branch # Checkout | ||
Line 25: | Line 39: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Delete/Remove a branch == | ==Delete/Remove a branch== | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
git branch -d <branch-name> | git branch -d <branch-name> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Remove last Commit and Push from the Remote == | ==Remove last Commit and Push from the Remote== | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
git reset --soft HEAD~1 | git reset --soft HEAD~1 | ||
Line 36: | Line 50: | ||
</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 == | ==Copy File from Another Branch== | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
git restore --source master ./assets/css/src/style.less | git restore --source master ./assets/css/src/style.less | ||
</syntaxhighlight>Where <code>master</code> is the name of the branch we want to copy from. | </syntaxhighlight>Where <code>master</code> is the name of the branch we want to copy from. | ||
== Hard Reset a Local Branch == | ==Hard Reset a Local Branch== | ||
<syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | <syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | ||
BRANCH="master" | BRANCH="master" | ||
Line 54: | Line 68: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Push a New Local Branch to a Remote Git Repository and Track It == | ==Push a New Local Branch to a Remote Git Repository and Track It== | ||
<syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | <syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | ||
BRANCH="master" | BRANCH="master" | ||
Line 63: | Line 77: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Stash and Pop Changes == | ==Stash and Pop Changes == | ||
<syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | <syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray"> | ||
BRANCH="master" | BRANCH="master" | ||
Line 85: | Line 99: | ||
</syntaxhighlight>After that you can commit, merge and push your changes. | </syntaxhighlight>After that you can commit, merge and push your changes. | ||
== References == | ==References== | ||
* Stack Overflow: [https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa How to delete all commit history in GitHub?] | *Stack Overflow: [https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa How to delete all commit history in GitHub?] | ||
* Stack Overflow: [https://stackoverflow.com/q/5613345/6543935 | *Stack Overflow: [https://stackoverflow.com/q/5613345/6543935 How to shrink the .git folder] | ||
* Stack Overflow: [https://stackoverflow.com/a/32833411/6543935 How to clone all repos at once from GitHub?] | *Stack Overflow: [https://stackoverflow.com/a/32833411/6543935 How to clone all repos at once from GitHub?] | ||
* 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?] | *Stack Overflow: [https://stackoverflow.com/a/60855504/6543935 How do I copy a version of a single file from one Git branch to another?] | ||
* Free Code Camp: [https://forum.freecodecamp.org/t/push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too/13222 Push a new local branch to a remote Git repository and track it too] | *Free Code Camp: [https://forum.freecodecamp.org/t/push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too/13222 Push a new local branch to a remote Git repository and track it too] | ||
* Atlassian | *Atlassian Git Tutorial: [https://www.atlassian.com/git/tutorials/undoing-changes Undoing Commits & Changes] | ||
* GitHub Docs: [https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message '''Changing a commit message'''] | *Atlassian Git Tutorial: [https://www.atlassian.com/git/tutorials/saving-changes/git-stash <code>git stash</code> - Saving Changes] | ||
*GitHub Docs: [https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message '''Changing a commit message'''] | |||
*How-To Geek: [https://www.howtogeek.com/devops/how-to-view-the-remote-url-a-github-repository-was-cloned-from/ View the Remote URL a Git Repository] | |||
*Super User: [https://superuser.com/questions/1419613/change-git-init-default-branch-name Change git init default branch name] | |||
* Stack Overflow: [https://stackoverflow.com/q/36358265/6543935 When does Git refresh the list of remote branches?] | |||
<noinclude> | <noinclude> | ||
<div id= | <div id="devStage"> | ||
{{devStage | {{devStage | ||
| Прндл = DevOps and SRE | |||
| Стадий = 6 | |||
| Фаза = Утвърждаване | |||
| Статус = Утвърден | |||
| ИдтПт = Spas | |||
| РзбПт = Spas | |||
| АвтПт = Spas | |||
| УтвПт = {{REVISIONUSER}} | |||
| ИдтДт = 5.07.2022 | |||
| РзбДт = 3.03.2023 | |||
| АвтДт = 3.03.2023 | |||
| УтвДт = {{Today}} | |||
| ИдтРв = [[Special:Permalink/27692|27692]] | |||
| РзбРв = [[Special:Permalink/32337|32337]] | |||
| АвтРв = [[Special:Permalink/32339|32339]] | |||
| РзАРв = [[Special:Permalink/32198|32198]] | |||
| УтвРв = {{REVISIONID}} | |||
| РзУРв = [[Special:Permalink/32215|32215]] | |||
}} | }} | ||
</div> | </div> | ||
</noinclude> | </noinclude> |
Latest revision as of 22:29, 3 March 2023
Git Config
The following command uses the --global
flag and will be applied to the file ~/.gitconfig
.
git config --global user.name "Spas Z. Spasov"
git config --global user.email "spas.z.spasov@example.com"
git config --global init.defaultBranch "master"
git config --global remote.origin.prune true
To apply an option to the repository's .git/config
, you need to remove the --global
flag.
git config user.email "spas.z.spasov@work.com"
git config remote.origin.prune false
Append a file to a branch
touch file.name
git add -A
git commit -am "my update msg"
git push
How to shrink the .git folder
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 commit history
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>
Remove last Commit and Push from the Remote
git reset --soft HEAD~1
git push -f origin HEAD^:BRANCH_NAME
Now you are ready to push the same changes by a new commit.
Copy File from Another Branch
git restore --source master ./assets/css/src/style.less
Where master
is the name of the branch we want to copy from.
Hard Reset a Local 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 Local Branch to a Remote Git Repository 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 returns something like: "You local changes will be overwritten… Please commit your changes or stash them before you switch the branches."
Then you can checkout new branch, like it is shown in the above topic: git checkout ‑b "$BRANCH"
. Then checkout the $BRANCH
again, or you can stash your changes, checkout the target $BRANCH
(without creating a new one) and then pop the stash them after the pull of the remote 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
After that you can commit, merge and push your changes.
References
- Stack Overflow: How to delete all commit history in GitHub?
- Stack Overflow: How to shrink the .git folder
- Stack Overflow: How to clone all repos at once from GitHub?
- DevConnected: How To Undo Last Git Commit
- Stack Overflow: How can I remove a commit on GitHub?
- Stack Overflow: How do I copy a version of a single file from one Git branch to another?
- Free Code Camp: Push a new local branch to a remote Git repository and track it too
- Atlassian Git Tutorial: Undoing Commits & Changes
- Atlassian Git Tutorial:
git stash
– Saving Changes - GitHub Docs: Changing a commit message
- How-To Geek: View the Remote URL a Git Repository
- Super User: Change git init default branch name
- Stack Overflow: When does Git refresh the list of remote branches?