Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

In the last article, we covered "Git Fetch and Git Pull". Today we are going to discuss another topic on the Git Basics tutorial series i.e. "How to stage your changes to Git repository", which you will find useful while working in Git.

 

Let's continue learning about the command "git add" today with the parameters that you need to know. Stay tuned to read more and more posts on Git basic topics.

 

Stage your changes to Git (www.kunal-chowdhury.com)

 

The git add and git commit commands compose the fundamental Git workflow. These are the two very basic commands that every Git user needs to learn, irrespective of the collaboration model that the team follows. These commands are used to commit new versions/changes of a project into the repository’s history.

 

First of all, you add/edit/delete your files in the working directory. When you’re ready to save, the git add command adds the changes in the working directory to the staging area. The staging area is a group of related changes into a single snapshot before actually committing it to the project history. When you are happy with the staged snapshot, you need to commit it to the actual project history with the command git commit, which we will discuss in the next post.

 

The 'git add' command updates the index using the current content found in the working tree. When you use this command, it actually prepares the said content staged for the next commit. In typical situation, it adds the current content of existing paths as a whole, but with some options you can also add content with only part of the changes made to the working tree or remove paths that do not exist in the working tree anymore.

 

That means, when you use the 'git add' command to a removed file path, it removes that path from the working tree. When you use it to a newly added file path, it adds the said path to the working tree. To stage the content, you need to specify 'git commit' command which we already discussed.

 

The "index" actually holds a snapshot of the content of the working tree. This snapshot then taken as the contents of the next commit. Thus after making any changes to the working tree and before running the 'git commit' command, you must use the 'git add' command to add any new or modified files to the index.

 

You can call this command multiple times before a commit. It only adds the content of the specified file(s) at the time the add command is run. If you want subsequent changes included in the next commit, you must run the 'git add' again to add the new content to the index.

 

You can use the 'git status' command to obtain a summary of the files having changes and staged for the next commit. Please note that the 'git add' command will not add any ignored files. It's by default. If any ignored files were explicitly specified on the command line, it will fail with a list of ignored files. You can add those ignored files with the -f or --force parameter/option.

 

Here's the basic commands that you need to learn and remember to add changes to the staging area:

 

Add specific file(s) or directory to the index:

$ git add <filepath>                  # You can add a pattern (global asterisk) to include matching files

$ git add <directory>

 

Update all files in the entire working tree:

$ git add -A

$ git add --all

 

Update specific files in the entire working tree:

$ git add -A <filepath>

$ git add --all <filepath>

 

Interactively choose hunks of patch between the index and the work tree:

$ git add -p <filepath>

$ git add --patch <filepath>

 

Pick and choose lines of the patch to apply:

$ git add -e <filepath>

$ git add --edit <filepath>

 

Update the index just where it already has an entry matching filepath:

$ git add -u <filepath>

$ git add --update <filepath>

 

Update the index by adding new files that are unknown to the index:

$ git add <filepath>

$ git add --no-all <filepath>

$ git add --ignore-removal <filepath>

 

Record only the fact that the path will be added later:

$ git add -N <filepath>

$ git add --intent-to-add <filepath>

 

Refresh the information in the index whithout adding the files:

$ git add --refresh <filepath>

 

Continue adding files without aborting on errors:

$ git add --ignore-errors

$ git add --ignore-errors <filepath>

 

Add modified contents in the working tree interactively:

$ git add -i

 

Was this post useful? If you came to this page by searching online about git basics, please have a look into my other blog posts. Subscribe to the RSS feed or the email newsletter to keep yourself updated.

 

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.