Git notes: Difference between revisions
mNo edit summary |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=== General git commands that are useful === | === General git commands that are useful === | ||
Change git remote: | == Change git remote:== | ||
With or without ssh keys added to your user account.. | With or without ssh keys added to your user account.. | ||
Line 10: | Line 10: | ||
</pre> | </pre> | ||
Sort git branches by last commit | == Sort git branches by last commit == | ||
<pre> | <pre> | ||
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' | git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' | ||
</pre> | </pre> | ||
Show git commit hashes for each branch sorted by date | == Show git commit hashes for each branch sorted by date == | ||
<pre> | <pre> | ||
git for-each-ref --sort=-committerdate refs/heads/ | git for-each-ref --sort=-committerdate refs/heads/ | ||
</pre> | </pre> | ||
Do a git diff between two branches | == Do a git diff between two branches == | ||
<pre> | <pre> | ||
git diff <branch>..origin/<branch2> | git diff <branch>..origin/<branch2> | ||
</pre> | |||
== Update Submodules == | |||
[https://stackoverflow.com/questions/1030169/pull-latest-changes-for-all-git-submodules pull-latest-changes-for-all-git-submodules] | |||
<pre> | |||
git submodule update --recursive --init | |||
git submodule update --recursive --remote | |||
</pre> | |||
== Checkout specific commit hash == | |||
This can be useful when the hash is not in the expected branch, or when you are in a detached head state | |||
<pre> | |||
git clone [remote_address_here] my_repo | |||
cd my_repo | |||
git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT] | |||
</pre> | |||
== Change submodule URL == | |||
This is very much a hammer way of doing this. I have seen elegant ways online, but they seem inconsistent when something goes wrong. This way is reproducible as far as I am concerned. | |||
This example assumes that directory/ is where your submodule lives. | |||
<pre> | |||
edit .gitmodule | |||
change values | |||
rm -rf directory/ | |||
git submodule update --init --recursive --remote | |||
cd into directory/ | |||
git pull whatever submodule branch you need | |||
cd .. | |||
git add -A | |||
git commit -m 'save new submodule changes' | |||
git push | |||
</pre> | |||
== Save image in README.md on github.com == | |||
Per: [https://stackoverflow.com/questions/14494747/how-to-add-images-to-readme-md-on-github Stackoverflow] | |||
Works: | |||
<pre> | |||
Very Simple : Can be done using Ctrl + C/V | |||
Most of the answers here directly or indirectly involve uploading the image somewhere else & then providing a link to it. | |||
It can be done very simply by just copying any image and pasting it while editing Readme.md | |||
Copying the image - You can just click on the image file and use Ctrl + C or may copy the screenshot image to your clipboard using the snipping tool | |||
You can then simply do Ctrl + V while editing Readme.md | |||
Guithub will automatically upload it to user-images.githubusercontent.com and a link to it will be inserted there | |||
</pre> | |||
== Change remote URL == | |||
[https://stackoverflow.com/questions/2432764/how-do-i-change-the-uri-url-for-a-remote-git-repository Change Git remote URL] | |||
<pre> | |||
git remote set-url origin new.git.url/here | |||
</pre> | |||
== Ignore local changes and pull == | |||
When you simply want to start over and nuke all your local mess.... | |||
[https://stackoverflow.com/questions/4157189/how-to-git-pull-while-ignoring-local-changes Stackoverflow on doing this] | |||
<pre> | |||
git fetch --all | |||
git reset --hard origin/<branch_name> | |||
</pre> | |||
== Clean up local orphan branches == | |||
When you have branches locally present that have been merged on the remote server this works to clean out your local repo. | |||
If there are still some that you want to keep, add in a 'grep -v "someName"' before xargs | |||
[https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote remove tracking branches no longer on remote] | |||
<pre> | |||
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d | |||
</pre> | |||
== Get latest commit hash for scripts == | |||
<pre> | |||
git rev-parse HEAD | |||
</pre> | |||
== Remove submodules of submodules == | |||
* This has been a recurring painful process. This is one solution that has worked for me | |||
[https://stackoverflow.com/questions/4185365/no-submodule-mapping-found-in-gitmodule-for-a-path-thats-not-a-submodule Stack Overflow discussion on this] | |||
<pre> | |||
git clone --recursive REPO | |||
cd down/to/submodule | |||
git checkout branch needed | |||
rm -f .gitmodules | |||
git submodule -init | |||
git submodule -update | |||
git rm --cached file/path/with/error/message | |||
git status | |||
git add -A | |||
git commit -m 'Remove submodules of submodules' | |||
git push | |||
cd parent repository | |||
cat .gitmodules | |||
Verify no references to submodules of submodules | |||
git submodule status --recursive | |||
git status | |||
git add -A | |||
git commit -m 'Remove references in submodule to nested submodules' | |||
git push | |||
</pre> | </pre> | ||
[[Category:Git]] | [[Category:Git]] |
Latest revision as of 10:52, 7 January 2025
General git commands that are useful
Change git remote:
With or without ssh keys added to your user account..
git remote set-url origin git@gitlab01.iwillfearnoevil.com:monitoring/nmsui.git or: git remote set-url origin https://gitlab01.iwillfearnoevil.com/monitoring/nmsui.git
Sort git branches by last commit
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
Show git commit hashes for each branch sorted by date
git for-each-ref --sort=-committerdate refs/heads/
Do a git diff between two branches
git diff <branch>..origin/<branch2>
Update Submodules
pull-latest-changes-for-all-git-submodules
git submodule update --recursive --init git submodule update --recursive --remote
Checkout specific commit hash
This can be useful when the hash is not in the expected branch, or when you are in a detached head state
git clone [remote_address_here] my_repo cd my_repo git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]
Change submodule URL
This is very much a hammer way of doing this. I have seen elegant ways online, but they seem inconsistent when something goes wrong. This way is reproducible as far as I am concerned.
This example assumes that directory/ is where your submodule lives.
edit .gitmodule change values rm -rf directory/ git submodule update --init --recursive --remote cd into directory/ git pull whatever submodule branch you need cd .. git add -A git commit -m 'save new submodule changes' git push
Save image in README.md on github.com
Per: Stackoverflow
Works:
Very Simple : Can be done using Ctrl + C/V Most of the answers here directly or indirectly involve uploading the image somewhere else & then providing a link to it. It can be done very simply by just copying any image and pasting it while editing Readme.md Copying the image - You can just click on the image file and use Ctrl + C or may copy the screenshot image to your clipboard using the snipping tool You can then simply do Ctrl + V while editing Readme.md Guithub will automatically upload it to user-images.githubusercontent.com and a link to it will be inserted there
Change remote URL
git remote set-url origin new.git.url/here
Ignore local changes and pull
When you simply want to start over and nuke all your local mess....
git fetch --all git reset --hard origin/<branch_name>
Clean up local orphan branches
When you have branches locally present that have been merged on the remote server this works to clean out your local repo.
If there are still some that you want to keep, add in a 'grep -v "someName"' before xargs
remove tracking branches no longer on remote
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Get latest commit hash for scripts
git rev-parse HEAD
Remove submodules of submodules
- This has been a recurring painful process. This is one solution that has worked for me
Stack Overflow discussion on this
git clone --recursive REPO cd down/to/submodule git checkout branch needed rm -f .gitmodules git submodule -init git submodule -update git rm --cached file/path/with/error/message git status git add -A git commit -m 'Remove submodules of submodules' git push cd parent repository cat .gitmodules Verify no references to submodules of submodules git submodule status --recursive git status git add -A git commit -m 'Remove references in submodule to nested submodules' git push