how to generate a patch which is a totally new file? - linux

When using git, I have a totally new file.
git diff
will show nothing.
I need to generate the diff content for other people.
ex:
git diff > 001.patch
How to let the 001.patch contains the new file's content?

A totally new file isn't tracked, and therefore wouldn't be shown by git diff. I'd stage it by using git add ./path/to/my/new/file.txt, along with git adding any other changes you may have, and then use git diff --cached.

git diff --no-index /dev/null /path/to/your/file.txt >001.patch After applying the patch they'll have an unstaged new file though.

Related

Apply all stashed changes within a subfolder

I had an awful list of old stashes
I have first removed the very old ones
git reflog expire --expire-unreachable=7.days refs/stash
I have one huge stash left, which contains many stashed changes. Some are to keep some other would damage my production system. I went through the diff
git diff stash#{0}^1 stash#{0}
and I know which ones are to keep
I could do
git checkout --patch stash#{0} -- myfilename
to unstash changes on myfilename and is working fine.
However, I have a large folder with many files with stashed changes inside. I would like to apply all of them but only within that subfolder.
I have tried to approach it using wildcards in ksh but I does not work
git checkout --patch stash#{0} -- myfolder/*
results in
error pathspec [...] did not match any files known to git
The solution does not need to be git based, can be a shell script to wrap git calls
Have you tried :
git checkout --patch stash#{0} -- myfolder
without the ending * ?
Chances are your shell expands myfolder/* before executing the git command, and lists the elements which currently exist on disk, which is probably not what you want.

What files am I working on?

A question I frequently ask my terminal is:
What files am I working on? (including committed files)
The answer is usually gotten by this command
git diff my_current_work_branch..master_branch | grep diff
this gives me a crude list of files that I'm currently working on.
Is there a more elegant way to get this info? All I want to ask git is,
what files does this branch add/modify/delete, relative to the master branch.
You can use git diff. If all you want is the names of files that differ, you can use the --name-only.
git diff --name-only master my_branch
or if you're actually checked out to my_branch
git diff --name-only master HEAD
or if you want to include uncommitted changes
git diff --name-only master
diffstat is a handy tool
git diff my_current_work_branch..master_branch` | diffstat -l
will show you each file that's been modified in the diff output.

Creating a zip file of files from repo for revert purposes

My normal process for deploying to an FTP server from git is this:
git status
git add .
git commit -m "Message"
zip update.zip $(git diff --name-only HEAD^) creates a zip file.
git push origin master
The zip file that is created is a list of all the updated files with their paths.
I'd like to do something similar that creates a zip file with the same content but instead listing the files before they were changed (so the previous commit for each). This way if there is any issue with any of the files I can quickly revert the changes.
Any suggestions?
First of all, instead of your zip update.zip $(git diff --name-only HEAD^) you can use git archive --format=zip -o update.zip which won't fail if you have whitespaces in your filenames. This command also take <tree-ish> parameter which is tree or commit to produce an archive for.
I do not quite understand what you are trying to achieve, but instead your (quietly complicated) workflow I would think about using something like git-ftp.

Git delete all unmodified files

I am using git in my project at Linux platform. I have plenty of files in a particular directory. I modified some 50 above files in that directory and didn't stage and commit it. I wish to delete all other unmodified files from that directory? Is there a way to do this, using git and Linux Commands?
Not sure why you would want to do this.... but you can:
# Save changes to stash
git stash save
# Remove everything left
rm -rf ./*
# Checkout (restore) all of the changed files
git stash show --stat | grep -v changed | sed -e 's/|.*$//;' | xargs git checkout
# Restore the changes to those files
git stash pop
git reset --hard [HEAD] should work for you repeated
Repeated question How can I discard modified files?
You can also use more simple commands for this purpose:
git clean -Xfd // capital X
git clean -xfd // lower x
It will clean your working directory from the desired files.
Using git clean is what you want. To remove (-x) those files and directories (-d), run:
$ git clean -fdx
If you use the -X option instead of -x, then the files you have told git to ignore will still be kept (e.g., build artifacts). Recent versions of git require either "-f" (force) or "-n" (dry-run) to be specified.
You should run a dry-run first, to show what will happen, but not actually do anything:
$ git clean -ndx
I use this so often, that I have an alias for this (added to your .gitconfig) to check for files that would be deleted when you run git clean. It's also useful to remind me if I've forgotten to "git add" a file that I want to keep.
[alias]
# list files that would be removed via 'clean' (non-destructive)
ifc = clean -ndx
Then, running git ifc (i.e,. "ifc" = "if clean") shows everything that isn't tracked and could be removed, or isn't tracked and should be added.
https://git-scm.com/docs/git-clean

How to automatically edit commits after "git-rebase -i"?

Very often I need to change the date of n previous commits. Usually I do git rebase -i #~20 and then in the editor manually change pick to edit, after that run in the loop commit --amend with desired date. It all works nicely, but I would like to automate the process so that the editor would not be called at all.
The question is: how do I switch to "edit mode" automatically after git rebase?
You can write a script that will behave like an editor and does what you want(it will be called with a temporary file and should modify it), then run the rebase with it - EDITOR=/path/to/your/script git rebase -i #~20
Also you might want to look onto git filter-branch approach suggested in How can one change the timestamp of an old commit in Git?

Resources