简体   繁体   中英

After checking git diff, how can I ignore changes in a few files that I dont want to add to this commit?

Consider I have made some changes in a few files or classes. FYI: I am an iOS Developer and use XCODE for development.

Now, After checking git diff from terminal, there are a few changes that I want to ignore and not add to the commit that I am about to make. How can I ignore changes in a few files that I dont want to add to the commit? And also, how to make sure that in future commits, it should not get added.

Thanks in advance...

There are two things you need to know about here:

First, how to ignore certain files permanently, even for future changes. This is typically done for files that are auto generated by your code, such as build files, or local settings (a swap file for vim for example). For this, you need to add the file patterns in a .gitignore file at the root of the repo directory. Read more on it here .

Second, how to ignore certain files for the following commit only. For this you can either hand pick the files you want to add by doing git add <file_name> , then the commit will only have these files, or you could stash your changes to the unwanted files, add all files, commit, then unstash. Read more on stashing here .

If these are files that you will never want to commit, then the answer is to use a .gitignore on your repo with the file patterns you want to ignore.

If you have, in the same file, some changes that you want to commit, and some that you don't, then you can do interactive staging . You start it with git add -i and typing p you select the patch option, git will ask you which file do you want to commit partially, you select the file and then git you show you every hunk of changes you made in the file and you select if you want to stage that hunk or not.

If you can use git-gui or another gui this process can be even simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM