Git Commit - Saving your changes using Git Commit

Git is the most popular version control system as of 2017 and hence it is an essential part of daily workflow of majority of software developers.

Git commit command is used for saving your changes in the git repository that can be later pushed to git remote repository.

Git Commit - Saving your Changes

Git supports 3 step process for saving (committing) your changes and pushing it to central repository. These steps include:

  1. Git Add - Identify files that need to be committed and use git add to mark them.
  2. Git Commit - commit to your local git repository by providing a commit message
  3. Git push - Push your local commit to remote

Advantage of this multi-step process is that it allows developer to selectively commit a subset of changes. For example, if you have made changes in 5 files but only want to commit 2 files - then you 'git add' only 2 files followed by 'git commit'

Git Add

Git add marks the files that need to be committed to git repository.

# add a file for commit
git add file_name1
git add file_name2

# see all files marked for commit
git status

Sometime, instead of typing individual files we want to add all files in a directory. One can use following for this:

# add all changes in a directory
git add directory_name

Git Commit

# commit all changes added using git add
git commit

# opens up text editor for commit message

# short cut: provide commit message on command line
git commit -m 'commit msg'

Git Push

Once you commit a change to your local repository, you need to push it to central repository. Otherwise your changes will not be visible to your colleagues.

# push local committed changes to remote branch git push origin branch_name