Stage and commit changes

suggest change

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

That opens a text editor (which editor is configurable; it is often Vim). In Vim, press i to insert, write the message, then press Esc and type :wq to save and quit. To skip the editor, pass -m:

git commit -m "Commit message here"

Commit messages often follow a format. See Good commit messages.

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        # same as git add -A

In Git 2.0 and later, this stages new files, modifications, and deletions under the current directory. Git 1.x did not stage deletions with git add .:

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"

That stages modifications and deletions of tracked files. It does not add untracked files.

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents