Git Basic Commands

Master the fundamental Git commands every developer needs to know: init, add, commit, and restore. Learn how to interact with Git through the command line effectively.

Git commands are how you interact with Git. They allow you to tell Git to perform specific actions. These commands are entered in your terminal (shell). This article covers the most basic commands that every developer should master.

Note:

If you haven't read the article on how Git works, please do so before reading this one, as it provides essential background knowledge.

Initializing Git in a Project

To initialize Git in a project, use the following command:

git init

This command will create a .git directory in the directory where you run it. For example, if you are in /my_git_project/, a .git directory will be created in that folder, and you can access it by going to /my_git_project/.git.

Note:

You might not see the .git directory in a regular ls listing because the . before git makes it a hidden directory. You can use the ls -al command to see hidden directories.

Now, Git is initialized in your project and ready to track your files!

Stage Changes for Commit

To stage files that have been modified, use the following command:

git add [file_name]

This command will place your file in the staging area, preparing it for the next commit.

Staging Individual Files

For example:

git add index.js

This will stage index.js, and you'll be able to include this file in your next commit.

Staging Directories

You can also stage an entire directory:

git add my_directory/

This command stages all files that have been modified in the specified directory.

Staging All Changes

If you want to add all your modified files to a commit, you can use:

git add .

Note:

Use with caution! This command stages all modified files from the directory where you run it. For example, if you run this command in /my_git_project/articles/, it stages all files in /my_git_project/articles/ and its subdirectories recursively.

It's best to avoid this approach unless you are only modifying one file at a time and committing changes in real-time.

Unstage a File

It's possible to make a mistake when staging files. For example, you might realize that you included a file that shouldn't be part of the commit you intended to make. In such cases, you can use the command:

git restore --staged [file_name_to_unstage]

Unstaging Multiple Files

You can also unstage entire directories:

git restore --staged my_directory/

Or unstage all files:

git restore --staged .

Note:

Important Warning: Use this command with caution. Without the --staged option, this command will reset the specified file or directory to its last known state (the state in your local repository), discarding all your current changes.

# This is DANGEROUS - it will discard your changes!
git restore index.js

Commit Changes

If you have files in your stage area, you probably want to make a commit. To do this, enter the following command:

git commit

This will open a text editor like Vi or Nano, depending on which one you have configured. This method can be a bit lengthy, which is why there is an option that streamlines the process:

git commit -m "commit message"

By adding the -m option, you can specify the commit message directly without opening an editor.

Example Commit

git commit -m "Implementation of my super useful function"

Note:

Your commit is now complete! The version of the file that was in the stage area has been copied to your local repository. The files that were previously in the stage area are no longer there since they have been committed.

Command Workflow Example

Here's a typical workflow combining these commands:

Conclusion

You should now be able to use Git at a basic level. These fundamental commands form the foundation of Git usage and are essential for any developer working with version control. The upcoming articles will focus on more advanced and concrete concepts like branching and collaborative workflows.

Summary

  • Commands are the way to interact with Git to perform actions
  • git init initializes Git in a directory, creating the .git folder
  • git add [file/directory] stages files for the next commit
    • Use specific file names for precise control
    • Use . to stage all changes (with caution)
  • git restore --staged [file/directory] unstages files from the staging area
    • Warning: Without --staged, this command discards changes permanently
  • git commit validates and saves staged changes to the repository
    • Use -m "message" to specify the commit message directly

Note:

In the next article, we'll explore Git branches to learn how to work on multiple features simultaneously without conflicts.