When sharing your code with others, there are often files or parts of your project, you do not want to share.
- log files
- temporary files
- hidden files
- personal files
Git can specify which files or parts of your project should be ignored by Git using a
Git will not track files and folders specified in
.gitignore. However, the
.gitignore file itself IS tracked by Git.
To create a
.gitignore file, go to the root of your local Git, and create it:
Now open the file using a text editor.
We are just going to add two simple rules:
- Ignore any files with the
- Ignore everything in any directory named
# ignore ALL .log files
# ignore ALL files in ANY directory named temp
.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
|Blank lines are ignored|
|# text comment||Lines starting with # are ignored|
|name||All name files, name folders, and files and folders in any name folder||/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.file||All files with the name.file||/name.file|
|/name.file||Starting with / specifies the pattern matches only files in the root folder||/name.file|
|lib/name.file||Patterns specifiing files in specific folders are always realative to root (even if you do not start with / )||/lib/name.file|
|**/lib/name.file||Starting with ** before / specifies that it matches any folder in the repository. Not just on root.||/lib/name.file|
|**/name||All name folders, and files and folders in any name folder||/name/log.file|
|/lib/**/name||All name folders, and files and folders in any name folder within the lib folder.||/lib/name/log.file|
|*.file||All files withe .file extention||/name.file|
|*name/||All folders ending with name||/lastname/log.file|
|name?.file||? matches a single non-specific character||/names.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|
|name[abc].file||[set] matches a single character in the specified set of characters (in this case either a, b, or c)||/namea.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|
|*.file||All files withe .file extention||/name.file|
|! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log||/name/file.txt|
|! specifies a negation or exception. All files withe .file extention, except name.file||/log.file|
|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
Local and Personal Git Ignore Rules
It is also possible to ignore files or folders but not show it in the distubuted
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
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:
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):
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:
clip < /Users/user/.ssh/id_rsa.pub
Go to GitHub, navigate to the top left corner, click your profile, and select: Settings:
Then select “SSH and GPG keys”. and click the “New SSH key” button:
Select a title, and paste the public SSH key into the “Key” field, and click “Add SSH Key”:
You will be prompted to supply your GitHub password.
You will see your new SSH key added:
Test SSH Connection to GitHub
Now we can test our connection via SSH to GitHub:
ssh -T [email protected] The authenticity of host 'github.com (18.104.22.168)' 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,22.214.171.124' (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:
Then use that address to add a new origin:
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
git remote set-url origin [email protected]:w3schools-test/hello-world.git