Before you can commit your work to local or remote repositories you need to make sure git is tracking the file you have changed, otherwise those changes won't sync. To add a specific file to the git repository run the command:
Now when you do a commit this file will be tracked and if you make changes to it in the future that will be tracked also. You only need to add the file once.
Now heres a short cut for you, if you want to add all the files in a directory you can run the following to save you a lot of time.
```bash
# Add all files in the git repo
git add .
```
> It is possible to have files in your working directory that are not tracked and pushed to the local and remote repositories.
### Excluding files
There are times when you want to exclude certain files from your git repository. These could be credential files or something more harmless like ```_DS_Store``` folders that get created when you look at images. A ```.gitignore``` file specifies intentionally untracked files that Git should ignore, and thus keeps those files from being commited even when you run ```git add .```. To create this file:
- Make sure you are in the root of your git repository
- create a new file called ```.gitignore```
- add the rules for the files to ignore
```
# OSx image cache
.DS_Store
# Some Java files you might want to ignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
```
- Now make sure to add the ```.gitignore``` file to git itself:
```bash
git add .gitignore
git commit -a
```
- If you want to ignore files that you've already added to the repo you need to run the following command:
```bash
git rm --cached <filename>
```
## Commits
When working with commits it's good practice to add a commit message that describes the changes you've made. This is also why it's good to make frequent commits and split your work into smaller units of work, meaning the commit message is simpler to create and more accurate. There are a coupole of ways you can add the commit message from the command line, the first isthe way I tend to use because you get to see a nice easy to read list of things that have changed and maybe files you've forgot to include. It opens up your default text editor for you to write the commit message in, yet again in my case this is vim. If you are wondering how to exit vim it's ``ESC`` then ``:wq``. This commit in this way do the following:
```bash
git commit -a
# Now enter you commit message at the top of the file and save and exit
```
However, some people prefer to add their commit message on the command line for speed and convenience. You can do this by adding the ```-m``` flag:
If you realise you've made a mistake and want to change the last commit, you don't have to create a new commit, you can use the ```-—amend``` flag to amend the most recent commit
> When you push changes you may need to use the ```-force``` flag but this can be dangerous so use cautiously.
As a rule of thumb it’s good practice to commit frequently, thus avoiding loss of progress or accidentally resetting the unstaged changes.
### Sync Commits to the Remote Repository
Right so you're commited this is good. If you remember back to our diagram about the stages of a git repository you've now moved the files into the local repository. It's now time to sync them back up to the remote repository. Now this is pretty simple just run the following command: