When you work with a program like Git, or other version control systems, “saving” changes is usually not as traditional as saving changes would be in something like Word or more traditional file editing applications. Today, I am going to show you how to save changes in Git using the “commit” command.
What is a Commit?
A “commit” is in fact, the Git version of saving. Traditional saving is thought of as a system operation used to overwrite an existing file or to write a new file. Git operates differently, as saving changes in Git involves committing an operation that in turn, acts on a collection of files and directories.
Git also has an additional saving mechanism known as “the stash.” Essentially, the stash is a storage area for changes that are not ready to be committed. The stash operates on the working directory and provides extensive options.
Furthermore, a Git repository can be configured for certain files and directories. This will prevent Git from saving changes to ignored content. Let’s take a look at how to save changes in Git.
Save Changes in Git
The git add and git commit commands compose the fundamental Git workflow. These are two commands that every Git user should know and understand. Essentially, these commands are the means to record versions of a project into the repository’s history.
See, developing a project on Git involves the basic edit/stage/commit pattern. So in simpler terms, it lays out like this:
Step 1: Edit Files in the Working Directory.
Edit all the files you have been working on and get them ready to “commit.”
Step 2: Use Git Add Comand
When you are satisfied, or ready, to save a copy of the current project as it is, then you stage changes with git add.
Step 3: Commit to Project History
Once you are happy with the staged snapshot that is provided you commit it to the project history with git commit.
Remember, git commit is saving changes in Git. You can also use the git reset command to undo a commit or staged snapshot when/if needed. There are some samples below so you can see what this would all look like.
In addition to git add and git commit, a third command called git push is essential for a complete and collaborative Git workflow. What git push does is send committed changes to remote repositories for collaboration. This allows other team members to access a set of saved changes.
The Staging Area
The main function of the git add command is to promote pending changes in the working directory, to the git staging area. See, the staging area is one of Git’s more unique features. If you are having a hard time understanding the staging area think of it like this:
The staging area is considered one of the three trees of Git. The other two trees are the working directory and the commit history. Instead of having to commit (save) all of the changes you have made since the last commit, the staging area lets you group related changes into highly focused snapshots before actually committing it to the project history.
This means you can make a bunch of edits to unrelated files, then go back and split them all up into logical commits by adding related changes to the stage and commit them piece-by-piece, instead of all together.
As is the case with any revision control system, it is important to create multiple commits so that it is easier to track down bugs and revert changes when necessary, without having a huge impact on the rest of the project.
Stage all changes in <file> for the next commit.
Stage all changes in <directory> for the next commit.
Begin an interactive staging session that lets you choose portions of a file to add to the next commit. This will present you with a chunk of changes and prompt you for a command. Here are some commands to choose from and what they mean:
- y: Use the y command to stage the chunk.
- n: Use the n command to ignore the chunk.
- s: Use the s command to split it into smaller chunks.
- e: Use the e command to manually edit the chunk.
- q: Use the q command to exit.
When you start a new project remember that git add serves the same function as svn import. To create an initial commit of the current directory, you will want to use the following two commands:
Once you have your new project up and running you can then add new files by passing the path to git add:
Use the commands above to record changes to existing files. Remember, Git does not differentiate between staging changes in new files vs. changes in files that have already been added to the repository.
Quick Review of Saving Changes
Git can be complicated unless you really start to work with it and understand the ins and outs of the program. Let’s do a quick review of saving changes in Git and how it works.
- The git add command is the first command to use in a chain of operations. This command directs Git to “save” a snapshot of the current project state, into the commit history. When it is used on its own, git add will promote pending changes from the working directory to the staging area, as described above.
- A git status command is used to examine the current state of your repository and can be used to confirm a git add promotion.
- The git reset command can be used to undo changes, essentially undo a git add command.
- The git commit command (also known as the save command) is then used to commit a snapshot of the staging directory (once you are happy with the snapshot) to the repositories commit history.
Saving changes in Git is a much different process than saving changes in other file editing applications. However, with a little practice and patience, you will have your Git project running smoothly in no time at all.
Keep practicing with Git and performing commits. You can also check out other Git commands as well. The system is vast, but like anything else, once you learn it then it can be very useful.
Author: Jeremy Holcombe
Growing up in Hawaii, Jeremy started his freelance writing career doing resumes, business plans, article writing, and everything in between. He now specializes in online marketing and content writing and is part of the Content Marketing Team at GreenGeeks.