Git Ignore and .gitignore – Git Advanced

Git Ignore

When sharing your code with others, there are often files or parts of your project, you do not want to share.

Examples

  • log files
  • temporary files
  • hidden files
  • personal files
  • etc.

Git can specify which files or parts of your project should be ignored by Git using a .gitignore file.

Git will not track files and folders specified in .gitignore. However, the .gitignore file itself IS tracked by Git.


Create .gitignore

To create a .gitignore file, go to the root of your local Git, and create it:

Example

touch .gitignore

Now open the file using a text editor.

We are just going to add two simple rules:

  • Ignore any files with the .log extension
  • Ignore everything in any directory named temp

Example

# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

Now all .log files and anything in temp folders will be ignored by Git.

Note: In this case, we use a single .gitignore which applies to the entire repository.

It is also possible to have additional .gitignore files in subdirectories. These only apply to files or folders within that directory.



Rules for .gitignore

Here are the general rules for matching patterns in .gitignore files: 

PatternExplanation/MatchesExamples
 Blank lines are ignored 
text commentLines starting with # are ignored 
nameAll name files, name folders, and files and folders in any name folder/name.log
/name/file.txt
/lib/name.log
name/Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder/name/file.txt
/name/log/name.log

no match:
/name.log
name.fileAll files with the name.file/name.file
/lib/name.file
/name.fileStarting with / specifies the pattern matches only files in the root folder/name.file

no match:
/lib/name.file
lib/name.filePatterns specifiing files in specific folders are always realative to root (even if you do not start with / )/lib/name.file

no match:
name.file
/test/lib/name.file
**/lib/name.fileStarting with ** before / specifies that it matches any folder in the repository. Not just on root./lib/name.file
/test/lib/name.file
**/nameAll name folders, and files and folders in any name folder/name/log.file
/lib/name/log.file
/name/lib/log.file
/lib/**/nameAll name folders, and files and folders in any name folder within the lib folder./lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file
*.fileAll files withe .file extention/name.file
/lib/name.file
*name/All folders ending with name/lastname/log.file
/firstname/log.file
name?.file? matches a single non-specific character/names.file
/name1.file

no match:
/names1.file
name[a-z].file[range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.)/names.file
/nameb.file

no match:
/name1.file
name[abc].file[set] matches a single character in the specified set of characters (in this case either a, b, or c)/namea.file
/nameb.file

no match:
/names.file
name[!abc].file[!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c)/names.file
/namex.file

no match:
/namesb.file
*.fileAll files withe .file extention/name.file
/lib/name.file
name/
!name/secret.log
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log/name/file.txt
/name/log/name.log

no match:
/name/secret.log
*.file
!name.file
! specifies a negation or exception. All files withe .file extention, except name.file/log.file
/lastname.file

no match:
/name.file
*.file
!name/*.file
junk.*
Adding new patterns after a negation will re-ignore a previous negated file
All files withe .file extention, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file

Local and Personal Git Ignore Rules

It is also possible to ignore files or folders but not show it in the distubuted .gitignore file.

These kinds of ignores are specified in the .git/info/exclude file. It works the same way as .gitignore but are not shown to anyone else.

Git Security SSH

Git Security

Up to this point, we have used HTTPS to connect to our remote repository.

HTTPS will usually work just fine, but you should use SSH if you work with unsecured networks. And sometimes, a project will require that you use SSH.


What is SSH

SSH is a secure shell network protocol that is used for network management, remote file transfer, and remote system access.

SSH uses a pair of SSH keys to establish an authenticated and encrypted secure network protocol. It allows for secure remote communication on unsecured open networks.

SSH keys are used to initiate a secure “handshake”. When generating a set of keys, you will generate a “public” and “private” key.

The “public” key is the one you share with the remote party. Think of this more as the lock.

The “private” key is the one you keep for yourself in a secure place. Think of this as the key to the lock.

SSH keys are generated through a security algorithm. It is all very complicated, but it uses prime numbers, and large random numbers to make the public and private key.

It is created so that the public key can be derived from the private key, but not the other way around.


Generating an SSH Key Pair

In rhe command line for Linux, Apple, and in the Git Bash for Windows, you can generate an SSH key.

Let’s go through it, step by step.

Start by creating a new key, using your email as a label:

Example

ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa):
Created directory '/Users/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/user/.ssh/id_rsa
Your public key has been saved in /Users/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:******************************************* [email protected]
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

You will be prompted with the following through this creation:

Enter file in which to save the key (/c/Users/user/.ssh/id_rsa):

Select a file location, or press “Enter” to use the default file location.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Entering a secure passphrase will create an additional layer of security. Preventing anyone who gains access to the computer to use that key without the passphrase. However, it will require you to supply the passphrase anytime the SSH key is used.

Now we add this SSH key pair to the SSH-Agent (using the file location from above):

Example

ssh-add /Users/user/.ssh/id_rsa
Enter passphrase for /Users/user/.ssh/id_rsa:
Identity added: /Users/user/.ssh/id_rsa ([email protected])

You will be prompted to supply the passphrase, if you added one.

Now the SSH key pair is ready to use.

Git GitHub Add SSH

Copy the SSH Public Key

In the previous chapter, we created an SSH key pair.

Now we will use the clip < command to copy the public key to our clipboard:

Example

clip < /Users/user/.ssh/id_rsa.pub

Go to GitHub, navigate to the top left corner, click your profile, and select: Settings:

GitHub Profile Settings

Then select “SSH and GPG keys”. and click the “New SSH key” button:

GitHub Profile Settings SSH

Select a title, and paste the public SSH key into the “Key” field, and click “Add SSH Key”:

GitHub Profile Settings SSH Add Key

You will be prompted to supply your GitHub password.

You will see your new SSH key added:

GitHub Profile Settings SSH Added Key

Test SSH Connection to GitHub

Now we can test our connection via SSH to GitHub:

Example

ssh -T [email protected]
The authenticity of host 'github.com (140.82.121.3)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.121.3' (RSA) to the list of known hosts.
Hi w3schools-test! You've successfully authenticated, but GitHub does not provide shell access.

If the last line contains your username on GitHub, you are successfully authenticated!


Add New GitHub SSH Remote

Now we can add a new remote via SSH to our Git.

First, get the SSH address from our repository on GitHub:

GitHub Get SSH Code

Then use that address to add a new origin:

Example

git remote add ssh-origin [email protected]:w3schools-test/hello-world.git

Note: You can change a remote origin from HTTPS to SSH with the command: git remote set-url remote-name [email protected]:username/repository.git

Example

git remote set-url origin [email protected]:w3schools-test/hello-world.git

Leave a Comment