Dear Readers,
What is GItIgnore in Git?
In this article,we will see Create new files and ignore specific file while commiting.
Git Ignore
A gitignore file specifies intentionally untracked files that Git should ignore.
Files already tracked by Git are not affected.
Git can be configured to ignore certain files and directories for repository operations.
The file system of Git is classified into three categories
- Tracked
- Untracked
- Ignored
Tracked
Tracked files are such files that are previously staged or committed.
Untracked
Untracked files are such files that are not previously staged or committed.
Ignored
Ignored files are such files that are explicitly ignored by git. We have to tell git to ignore such files.
I am creating 4 files ktexperts.txt,ktexperts.java,ktexperts.class,ktexperts.php
In this 4 files I am going to ignore .class file while commiting.
To ignore the file while commiting
.gitignore
Create a new file “.gitignore” and keep content like pattern matching “*.class”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# vi .gitignore [root@ip-172-31-42-20 mumbaigit]# cat .gitignore *.class |
To see the .gitignore file and see the status of git
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# ls -a . .git myfile .. .gitignore #To see the status of git# [root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore nothing added to commit but untracked files present (use "git add" to track) |
Adding to Staging Area
Add
[root@ip-172-31-42-20 mumbaigit]# git add .
Commit the file into Local Repository
Commit
1 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m ".gitignore" |
Note
Now onwards,if you create any file with extension of .class git will ignore those files automatically.
Create 4 new files
By using touch command.
We create 4 new files “ktexperts.txt,ktexperts.java,ktexperts.class,ktexperts.php”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# touch ktexperts.txt ktexperts.class ktexperts.java ktexperts.php [root@ip-172-31-42-20 mumbaigit]# ls ktexperts.class ktexperts.java ktexperts.php ktexperts.txt myfile |
To see the status of git
It is not showing ktexperts.class files because git has ignored that file.
If run add these 3 files only will add to staging area expect ktexperts.class.
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts.java ktexperts.php ktexperts.txt nothing added to commit but untracked files present (use "git add" to track) |
Adding to Staging Area
Add
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git add . [root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts.java new file: ktexperts.php new file: ktexperts.txt |
Commit the file into Local Repository
Commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "3files commited" [master 3228067] 3files commited 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 ktexperts.java create mode 100644 ktexperts.php create mode 100644 ktexperts.txt |
Create one more file “DevOps.class”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# touch devops.class [root@ip-172-31-42-20 mumbaigit]# ls devops.class ktexperts.class ktexperts.java ktexperts.php ktexperts.txt myfile |
To see the status git
We can see nothing to commit means git has ignored DevOps.class file.
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master nothing to commit, working tree clean |
Ignoring files manually while commiting
Make new files
Create new files “file1.java file2.java file3.java file1.txt file2.txt file3.txt file1.php file2.php file3.php”
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# ls devops.class ktexperts.class ktexperts.java ktexperts.php ktexperts.txt myfile [root@ip-172-31-42-20 mumbaigit]# touch file1.java file2.java file3.java file1.txt file2.txt file3.txt file1.php file2.php file3.php [root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.php file1.java file2.java file3.java ktexperts.class ktexperts.txt file1.php file2.php file3.php ktexperts.java myfile |
To see the status of git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: file1.java new file: file1.php new file: file1.txt new file: file2.java new file: file2.php new file: file2.txt new file: file3.java new file: file3.php new file: file3.txt |
Adding files to Staging Area
Here,we add files to staging area(want to add) and ignore (don’t want to add).
Mention required file names.
we shouldn’t mention the file names which is you don’t want to add.
If you don’t specify file so that file won’t be added to staging area.
Add
We can add files to staging area by using “*”
[root@ip-172-31-42-20 mumbaigit]# git add *.txt *.java *.php
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: file1.java new file: file1.php new file: file1.txt new file: file2.java new file: file2.txt new file: file3.java new file: file3.txt |
Commit into Local Repository
Commit
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "commited by using *" [master 055e453] commited by using * 9 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1.java create mode 100644 file1.txt create mode 100644 file2.java create mode 100644 file2.php create mode 100644 file2.txt create mode 100644 file3.java create mode 100644 file3.txt |
Git log Options
To see the list of all commits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
[root@ip-172-31-42-20 mumbaigit]# git log commit 055e453f97d5fb25efb127421f5d79046bf36100 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:39:12 2019 +0000 committed by using * commit 32280677a92f6a558854a533f9007ddf8437e3a1 Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:29:26 2019 +0000 3 files commited commit d3f38fcbadf6706543856b1360addae205d06a4d Author: Ram <ram@gmail.com> Date: Mon Dec 16 18:46:01 2019 +0000 .gitignore commit 98a8f74b00ab57708dcb7e5a1c92dfd3f532607d (origin/master) Author: Ramesh-Ktexperts <58362036+Ramesh-Ktexperts@users.noreply.github.com> Date: Mon Dec 16 23:39:17 2019 +0530 Delete myfile1 commit 3f8608da4557a610133cb9ab208af06b80ec37fd Author: Ram <ram@gmail.com> Date: Mon Dec 16 13:20:30 2019 +0000 ramesh commit 48435c663271473ea9d13127affe41f833df4167 Author: Ram <ram@gmail.com> Date: Mon Dec 16 13:17:52 2019 +0000 6th commit from mumbai commit 098682efcc95f894f232b54b69e82ee65f6c6823 Author: Hanuma <hanuma.ktexperts@gmail.com> Date: Mon Dec 16 12:31:35 2019 +0000 5th commit from london commit c3406c400598c9315afe52ce2c030f99559f5493 Author: Ram <ram.ktexperts@gmail.com> Date: Sat Dec 14 06:30:34 2019 +0000 3rd commit from mumbai commit 731a4475e861543eb38a79700f9838a766b1be39 Author: Hanuma <hanuma.ktexperts@gmail.com> Date: Sat Dec 14 06:17:09 2019 +0000 1st commit from london commit f7e5d55c9daaef9bcb9099062ccffb99b93879e1 Author: Ram <ram.ktexperts@gmail.com> Date: Sat Dec 14 05:25:21 2019 +0000 2nd commit from mumbai commit cbb38f20f70e7443eea2c3daa54fb7ae7f264873 Author: Ram <ram.ktexperts@gmail.com> Date: Fri Dec 13 17:34:33 2019 +0000 1st commit from mumbai |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit 055e453f97d5fb25efb127421f5d79046bf36100 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:39:12 2019 +0000 committed by using * |
To see the latest 2 commits
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git log -2 commit 055e453f97d5fb25efb127421f5d79046bf36100 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:39:12 2019 +0000 commited by using * commit 32280677a92f6a558854a533f9007ddf8437e3a1 Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:29:26 2019 +0000 3files commited |
To see the latest 3 commits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-20 mumbaigit]# git log -3 commit 055e453f97d5fb25efb127421f5d79046bf36100 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:39:12 2019 +0000 commited by using * commit 32280677a92f6a558854a533f9007ddf8437e3a1 Author: Ram <ram@gmail.com> Date: Tue Dec 17 03:29:26 2019 +0000 3files commited commit d3f38fcbadf6706543856b1360addae205d06a4d Author: Ram <ram@gmail.com> Date: Mon Dec 16 18:46:01 2019 +0000 .gitignore |
Note
We can give any number instead of 1 or 2 or 3
Based on the number it will display the list of commits.
To see all the list of commits in a summarized manner
It will show only first 7 characters of commit ID and commit messages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 055e453 (HEAD -> master) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai To Pick commit based on commit message [root@ip-172-31-42-20 mumbaigit]# git log --oneline 055e453 (HEAD -> master) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
Pick the commit by using grep
It will show the commits which belongs to commit message “london”
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git log --grep "london" commit 098682efcc95f894f232b54b69e82ee65f6c6823 Author: Hanuma <hanuma.ktexperts@gmail.com> Date: Mon Dec 16 12:31:35 2019 +0000 5th commit from london commit 731a4475e861543eb38a79700f9838a766b1be39 Author: Hanuma <hanuma.ktexperts@gmail.com> Date: Sat Dec 14 06:17:09 2019 +0000 1st commit from london |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Follow Me
Ramesh’s Linkedin : https://www.linkedin.com/in/ramesh-atchala/
Priya
Best learning platform for devops students
santhi
By seeing this article I came to get knowledge on GitIgnore in DevOps.
Thanks for making and It has great information.