Git Committing Stage and commit changes

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The basics

After making changes to your source code, you should stage those changes with Git before you can commit them.

For example, if you change README.md and program.py:

git add README.md program.py

This tells git that you want to add the files to the next commit you do.

Then, commit your changes with

git commit

Note that this will open a text editor, which is often vim. If you are not familiar with vim, you might want to know that you can press i to go into insert mode, write your commit message, then press Esc and :wq to save and quit. To avoid opening the text editor, simply include the -m flag with your message

git commit -m "Commit message here"

Commit messages often follow some specific formatting rules, see Good commit messages for more information.


Shortcuts

If you have changed a lot of files in the directory, rather than listing each one of them, you could use:

git add --all        # equivalent to "git add -a"

Or to add all changes, not including files that have been deleted, from the top-level directory and subdirectories:

git add .

Or to only add files which are currently tracked ("update"):

git add -u

If desired, review the staged changes:

git status           # display a list of changed files
git diff --cached    # shows staged changes inside staged files

Finally, commit the changes:

git commit -m "Commit message here"

Alternately, if you have only modified existing files or deleted files, and have not created any new ones, you can combine the actions of git add and git commit in a single command:

git commit -am "Commit message here"

Note that this will stage all modified files in the same way as git add --all.


Sensitive data

You should never commit any sensitive data, such as passwords or even private keys. If this case happens and the changes are already pushed to a central server, consider any sensitive data as compromised. Otherwise, it is possible to remove such data afterwards. A fast and easy solution is the usage of the "BFG Repo-Cleaner": https://rtyley.github.io/bfg-repo-cleaner/.

The command bfg --replace-text passwords.txt my-repo.git reads passwords out of the passwords.txt file and replaces these with ***REMOVED***. This operation considers all previous commits of the entire repository.



Got any Git Question?