How to diff before each commit without going into interactive mode? - bash

Say you do git status and you get back a list of files which you want to check one by one and then commit them one by one.
Or you do a git diff on a directory and want to go and commit directly after your inspection. How to do that with a simple trick?

If you are in a GUI environment. You can launch "git gui" from the console..
This would launch a window, where the left pane would contain the list of changed files. Selecting any of them, shows you the diff.
You can add files by clicking on the icon to the left of the file name and later commit it from the same window.
Hope it helps.

Note: diff + commit would not work because you have not added anything to the index.
As commented, git add -i or git add -p (--patch) will give you a diff view, with the possibility to add or skip each diff hunk.
Then you can commit the modified index.

UPDATE 2020:
After fiddling around I decided to install and use ydiff.
Doing ydiff -s shows a diff side by side in the terminal.
No brainer check it out here and don't mind my old answer:
https://github.com/ymattw/ydiff
[ OLD
]
Git diff before your commit?
Simple solution thinking outside of the box:
Stick this in your .bashrc or .zshrc:
gdiffy() { git diff $1 && git add $1 && git commit $1; }
First it will pop open up a diff, when you close it, it will allow you enter a commit message.
usage example:
gdiffy config/database.yml
or
gdiffy .

Related

Watch for file changes and add changes to github?

On OSX I want to create a script that 'when a file changes, automatically runs
git add [FILES CHANGED];
git commit -m "Changing ${file names}";
git push -u origin master"
I understand there is fswatch, however, this keeps a process running.
Maybe I can try running "git status" and if != "On branch master
nothing to commit, working directory clean" then automatically push to Github?
I know this is an old thread, but I was also looking for an alternative solution and found rerun2. Perhaps it will also be useful to somebody else.
https://github.com/tartley/rerun2
From Tartley's readme:
It runs COMMAND every time there's a filesystem modify event within
your current directory (recursive). For example, it can re-run tests
every time you hit 'save' in your editor. Or it can re-generate
rendered output (html, SVG, etc) every time you save the source (e.g.
Markdown or GraphViz dot).
COMMAND needs to be a single parameter to rerun, hence if it contains
spaces, either quote the whole thing:
rerun "ls ~"
or escape them:
rerun ls\ ~
How to implement:
If you take your code, and put it inside an update.sh script:
#!/usr/bin/env bash
git add -u
git commit -m "Automatic update of modified files" && git push
Make it executable:
chmod u+x gitpush.sh
Create an alias to make things easier:
alias gitpush=/path/to/gitpush.sh:
Open the terminal, got to the directory you want to watch and run the command:
rerun gitpush
Now it will execute gitpush everytime files changes in that directory.
If you're willing to give up automatically responding to file modifications, you could just periodically run (e.g., via a crontab entry or something):
git add -u
git commit -m "Automatic update of modified files" && git push
The git add -u will stage any modified files, and "git commit" will only be successful if there are modifications to commit.
If you also want to pick up new files, you could use git add -A instead.

How to discard uncommitted changes in SourceTree?

I'm new to the Git environment, and I'm using BitBucket with SourceTree on Mac. All I want to do now is to discard the changes since last commit. How should I do this? I haven't found anything like "discard changes", and directly pulling from the last commit doesn't seem to work. Solutions done with either the GUI or command line will be good.
On SourceTree for Mac, right click the files you want to discard (in the Files in the working tree list), and choose Reset.
On SourceTree for Windows, right click the files you want to discard (in the Working Copy Changes list), and choose Discard.
On git, you'd simply do:
git reset --hard to discard changes made to versioned files;
git clean -xdf to erase new (untracked) files, including ignored ones (the x option). d is to also remove untracked directories and f to force.
I like to use
git stash
This stores all uncommitted changes in the stash. If you want to discard these changes later just git stash drop (or git stash pop to restore them).
Though this is technically not the "proper" way to discard changes (as other answers and comments have pointed out).
SourceTree: On the top bar click on icon 'Stash', type its name and create. Then in left vertical menu you can "show" all Stash and delete in right-click menu. There is probably no other way in ST to discard all files at once.
Follow steps to discard multiple uncommited changes in Sourcetree.
New version of Sourcetree does not have -Reset Button- as mentioned previous answer. Thus, please follow these 5 steps for solution.
Right click "File status" and click "Reset...".
Select files. If you want, you can select all of them like the below image.
Click "Reset All".
Again click "Reset All".
Click "Reset".
Welldone..! No more 302 files to discard.
On the unstaged file, click on the three dots on the right side. Once you click it, a popover menu will appear where you can then Discard file.
Ok I just noticed that my question was already answered in the question title.
To unstage files use
git reset HEAD /file/name
And to undo the changes to a file
git checkout -- /file/name
If you have a batch of files inside a folder you can undo the whole folder
git checkout -- /folder/name
Note that all these commands are already displayed when you git status
Here I created a dummy repo and listed all 3 possibilities
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test2
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test3
From sourcetree gui click on working directoy, right-click the file(s) that you want to discard, then click on Discard
Ok so in Windows sourcetree that is simple, on macOS I looked as well for a while..
Click Command + Shift + R while in source tree a hidden popup will be shown that will let you discard individual files OR ALL! Why is this hidden? We will never know.. but it works!
Do as follow,
Click on commit
Select all by pressing CMD+A that you want to delete or discard
Right click on the selected uncommitted files that you want to delete
Select Remove from the drop-down list
It's Ctrl + Shift + r
For me, there was only one option to discard all.

Textmate shows files in the project folder that have been deleted

This might be a silly question, but can we prevent the displaying of old references in the project list view? I see files that were deleted but still show with an "X" icon. I wish I could attach a screenshot.
As per the comments, this is a git issue, and not a textmate issue.
Try this:
$ git commit -a -m "git comment"
$ git push origin master
According to the OP, this will also work:
$ git add -A
Reference:
Textmate 2 Alpha showing references for deleted files

How do I make to some of the files that I have changed is not offered in the commit?

I use TortoiseGit 1.8.3.
I changed one of the files: Makefile, but I want to not offer commit it to me every once in a Git Commit.
I added it to the "delete and add to ignore list", but it does not help.
How do I make to some of the files that I have changed is not offered in the commit?
I want, that Makefile was in remote repository as read-only, that I could edit it locally, and then TortoiseGit does not offer to me to commit it.
That I wanted for Git-bash: https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html
Answer for TortoiseGit 1.8.15, Git 2.6.1. There is no need to revert to the command line, all functions are directly available in TortoiseGit. I've tried to summarize the various ways how this function is accessible.
I assume that the reader knows what "assume unchanged" means. Here is easy documentation about this feature. Or the original documentation about --assume-unchanged or git ls-files.
Flag a file as "assume unchanged"
There are three possibilities in TortoiseGit: in the Commit dialog, the Working Tree dialog (Check for Modifications) or in the Log Messages dialog (only when Working dir changes entry is selected). From one of these dialogs do the following:
right-click the file and select the entry Assume Unchanged
From any file list in Windows Explorer do the following:
right-click the file and select properties
go to the Git tab
tick the entry Assume valid/unchanged
Remove the "assume unchanged" flag from a file (undo the above):
TortoiseGit allows to remove the flag only from the Working Tree dialog (check for Modifications).
in the lower left corner of the dialog: make sure to tick the option Show ignore local changeds flagged files
all files with assumed valid or skip worktree flag will be shown below the normal changed files
right-click on the file and select Unflag as skip-worktree or assume-unchanged
From any file list in Windows Explorer do the following:
right-click the file and select properties
go to the Git tab
untick the entry Assume valid/unchanged
If I'm interpreting the question correctly, Alex wants to know how to undo an --assumed-unchanged action done using Tortoise Git.
You don't need to use command line Git to fix this:
Navigate to the file(s) you did this action on in windows explorer,
Right-click and choose Properties,
Select the Git tab, there you should see a checked box next to "Assume valid/unchanged".
Uncheck it and it won't be ignored by commits moving forward.
The approved answer is good but in case you're hunting for the dialogues and options here you have a short help:
If it doesn't help, then you need to go to a command-line interface, and check:
the content of the .gitignore file that you should find at the root directory of your repo (as mentioned in "TortoiseGit: hide/remove files never to be versioned")
the result of git rm --cached Makefile.
the result of git status.
I want, that Makefile was in remote repository in read-only, that I could edit it localy, and then TortoiseGIT does not offer to me to commit it.
This is different:
You need:
git update-index --really-refresh --no-assume-unchanged Makefile
That will make any local modification to Makefile "invisible" to git, while keeping Makefile under source control.
If you want your local modifs to resist a git reset, do;
git update-index --skip-worktree Makefile

Can't ignore UserInterfaceState.xcuserstate

I'm using Git for Xcode 4 project version control. I've explicitly added ProjectFolder.xcodeproj/project.xcworkspace/xcuserdata/myUserName.xcuserdatad/UserInterfaceState.xcuserstate to .gitignore, but Git it won't ignore it. Any ideas why this is so?
Git is probably already tracking the file.
From the gitignore docs:
To stop tracking a file that is currently tracked, use git rm --cached.
Use this, replacing [project] and [username] with your info:
git rm --cached [project].xcodeproj/project.xcworkspace/xcuserdata/[username].xcuserdatad/UserInterfaceState.xcuserstate
git commit -m "Removed file that shouldn't be tracked"
Alternatively you can use the -a option to git commit that will add all files that have been modified or deleted.
Once you've removed the file from git, it will respect your .gitignore.
In case that the ignored file kept showing up in the untracked list, you may use git clean -f -d
to clear things up.
1.
git rm --cached {YourProjectFolderName}.xcodeproj/project.xcworkspace/xcuserdata/{yourUserName}.xcuserdatad/UserInterfaceState.xcuserstate
2.
git commit -m "Removed file that shouldn't be tracked"
3.
WARNING first try git clean -f -d --dry-run, otherwise you may lose uncommited changes.
Then:
git clean -f -d
All Answer is great but here is the one will remove for every user if you work in different Mac (Home and office)
git rm --cache */UserInterfaceState.xcuserstate
git commit -m "Never see you again, UserInterfaceState"
Had a friend show me this amazing site https://www.gitignore.io/. Enter the IDE of your choice or other options and it will automatically generate a gitignore file consisting of useful ignores, one of which is the xcuserstate. You can preview the gitignore file before downloading.
In case the file keeps showing up even after doing everything mentioned here, make sure that this checkbox in Xcode settings is unchecked:
Just
"git clean -f -d"
worked for me!
Here are some demo & short cuts if you uses GitHub, the basic ideas are the same.
1. Open terminal like this
2. Paste the below command to terminal followed by a space and then paste the path of the .xcuserstate file simply like this
git rm --cached
3. Make sure you have the correct git ignore and then commit the code :)
This works for me
Open the folder which contains the project file project.xcworkspace from the terminal.
Write this command: git rm --cached *xcuserstate
This will remove the file.
For me nothing worked, but this
add this line to your gitignore
*.xcuserdata
Here is one more simple solution if you are using the source tree app.
here are the instructions
1.Right-click on the file which you want to add to the git ignore list and select stop tracking.
again right-click on the same file and you will notice ignore option is now enabled then click on ignore button.
now you can reset or commit your changes for the same file it depends on whether your changes are important or not. changes in the future will not be tracked for the selected file.
Here is a very nice explanation of how to remove the files in question recursively from your git history: http://help.github.com/remove-sensitive-data/
Very useful, because otherwise tools tend to 'hang' while trying to show the diff on those huge files that shouldn't have been checked in the first place...
Here's what you can do (in short) to get rid of the largest stuff:
cd YourProject
git filter-branch --index-filter 'git rm --cached --ignore-unmatch -r YourProject.xcodeproj/project.xcworkspace' HEAD
# see what you want to do with your remote here...
# you can: git push origin master --force
# or you can delete it and push a fresh new one from your cleaned-up local...
rm -rf .git/refs/original
git gc --prune=now
git gc --aggressive --prune=now
Worked very nicely for me :)
For xcode 8.3.3 I just checked tried the above code and observe that, now in this casewe have to change the commands to like this
first you can create a .gitignore file by using
touch .gitignore
after that you can delete all the userInterface file by using this command and by using this command it will respect your .gitignore file.
git rm --cached [project].xcworkspace/xcuserdata/[username].xcuserdatad/UserInterfaceState.xcuserstate
git commit -m "Removed file that shouldn't be tracked"
You can also ignore files from Xcode preferences itself.
Generate gitignore file from https://www.toptal.com/developers/gitignore
Go to Xcode -> Preferences -> Source Control -> Git -> Add all ignore items in the list...Even though UI is not really useful & you have to add all items individually but adding ignore files here surely works.
I think it would be better to write like this.
git rm --cache *//UserInterfaceState.xcuserstate**

Resources