Difference between revisions of "Git/hacks"

From Freephile Wiki
< Git
Jump to navigation Jump to search
(add story of merging remotes)
Line 80: Line 80:
 
</source>
 
</source>
  
 +
== Unbloat ==
 +
https://stackoverflow.com/questions/3797907/how-to-remove-unused-objects-from-a-git-repository
 +
 +
<source lang="bash">
 +
git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 \
 +
  -c gc.rerereresolved=0 -c gc.rerereunresolved=0 \
 +
  -c gc.pruneExpire=now gc
 +
</source>
 
{{References}}
 
{{References}}
  
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 21:39, 17 November 2016

Add GIT_TRACE=1 to your command to see more of what's going on. (Note that there are no spaces and no semicolon.)

GIT_TRACE=1 git status

See all the changes in color, but without any context lines, and without the leading +/-/[space] This makes it easy to grab changes and stuff them in another file for example.

git diff -U0 --color myfile | sed -r "s/^([^-+ ]*)[-+ ]/\\1/"


You forgot to add a file to the last commit? Just add it to the index, and commit with --amend. Added a file that shouldn't be there? git rm it. If you leave off the -m (message) option in the new commit, it will let you re-use the last commit message. This lets you "undo the last commit" and redo it right. You usually do not want to amend a commit if you've already pushed it to other repos, but if it's just local --amend is awesome-sauce.

git add forgotten.php
git rm oops.txt
git commit --amend
git log --stat

What's the commit history? --stat gives a nice view of what happened in the log.

git log --stat

How do you ignore a directory, but make an exception? What if you already added certain directories to git but want to stop tracking them now (ie. "take them out of version control")? A combination of editting your .gitignore file and git rm --cached to the rescue. I had accidentally added and committed some files into git which should have been ignored because they are 3rd party files managed by Composer. I fixed my .gitignore to track only what I want while ignoring a parent directory:

/nbproject/*
# ignore everything in the 'vendor' directory
/vendor/*
# but don't ignore the 'eqt' directory
!/vendor/eqt/

Then you simply remove all files from git's index, and add them back (only now adding them back will look to .gitignore for the corrected rules)

git rm -r --cached .
git add .
git commit -m 'ignoring vendor/*'

Tags[edit | edit source]

With git, you can just tag something with git tag foo. This produces a 'lightweight' tag [1]. Use "annotated tags" whenever you want to know when something was tagged and who did it. Pass an empty message if you really don't care or need extra annotation.

git tag -am '' 'REL-1.0-alpha'


Git merge branch of another remote[edit | edit source]

When using git between a local repository and a single 'origin' remote, it's a simple process to work locally and push things back up to origin. But, what if you have a separate remote repository... perhaps on GitHub, or a collaborator who has similar sources but not using your origin (so disconnected, and perhaps not even linked ancestrally like a fork). How do you add that other remote to your project and then pull in the code "they" have on top of yours? Here's an example of how we started with a repo from github and added a repo that we were developing privately. (The reality is that we were developing a repo privately; created a sibling version of the code at github; and then wanted to re-incorporate the changes of the github repo back into our private repo.)

# start with one 'origin' remote
git clone https://github.com/freephile/qb.git
# add another remote that has similar code
git remote add private greg@eqt:/home/greg/src/ad.git
# check
git remote -v
# rename it for the host it came from
git remote rename private eqt
# pull our 'eqt' remote onto the master branch of the code we got from 'origin'
git pull eqt master
# But there are some unversioned files in the way (git complains)
# So, create a new 'dev' branch and stuff all the new things there (which we can either delete, or resume later)
git checkout -b dev
git add hosts package.json requirements.txt scripts/
git commit -m 'stashing some files into a dev branch'
git checkout master
# now we can merge again
git pull eqt master
# git complains about some merge conflicts (changes on both sides so it can't just do a fast-forward)
# edit those files to remove the conflict markers and get them the way you want them
git add install* launch.yml
# finish your merge by committing the result
git commit -m 'merged additional plays from eqt'
# and push upstream to 'origin'
git push
# and push to other remote 'eqt'
git push eqt

Unbloat[edit | edit source]

https://stackoverflow.com/questions/3797907/how-to-remove-unused-objects-from-a-git-repository

git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 \
  -c gc.rerereresolved=0 -c gc.rerereunresolved=0 \
  -c gc.pruneExpire=now gc

References[edit source]