Git Committing
$ git commit -m "your message"
Making a commit is similar to making a save in a file. We're taking a snapshot of a git repository in time. When saving a file, we are saving the state of a single file. With Git, we can save the state of multiple files and folders together.
We use the git commit command to actually commit changes from the staging area. When making a commit, we need to provide a commit message that summarizes the changes and work snapshotted in the commit. The -m flag allows us to pass in an inline commit message
Undoing Things
$ git commit --amend
One of the common undos takes place when we commit too early and possibly forget to add some files, or we mess up our commit message. If we want to redo that commit, make the additional changes we forgot, stage them, and commit again using the --amend option.
lets say we commit and then realize that we forgot to stage the changes in a file we wanted to add to this commit, we can do something like this
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
The same commit-message editor fires up, but it already contains the message of our previous commit. we can edit the message the same as always, but it overwrites our previous commit.
Some Basic Guidelines
- Commit early and often.
- a commit should encompass a single feature, change, or fix. In other words, try to keep each commit focused on a single thing. This makes it much easier to undo or rollback changes later on. It also makes our code or project easier to review.
- Write meaningful but concise commit messages.
Ignoring Files
We can tell Git which files and directories to ignore in a given repository, using a .gitignore file.This is useful for files we know we NEVER want to commit, including:
- Secrets, API keys, credentials, etc..
- Operating System files (.DS_Store on Mac).
- Log files.
- Dependencies & packages
Create a file called .gitignore in the root of a repository. Inside the file, we can write patterns to tell Git which files & folders to ignore:
- DS_Store will ignore files named .DS_Store
- folderName/ will ignore an entire directory.
- *.log will ignore any files with the .log extension.