Making changes in code and creating commits in Git

Photo by Yancy Min on Unsplash

Making changes in code and creating commits in Git

Making changes in your code and creating commits is a fundamental aspect of using Git for version control. Commits represent snapshots of your project's state at specific points in time and help you track the history of your changes. Here's how you make changes and create commits in Git:

  1. Edit Your Code:

    • Open your code files in a text editor or an IDE.

    • Make the desired changes to your code, such as adding new features, fixing bugs, or updating documentation.

  2. Check the Status:

    • Before creating a commit, it's a good practice to check the status of your working directory. Use the git status command to see which files have been modified.
  3. Stage Changes:

    • Use the git add command to stage the changes you want to include in the next commit. Staging prepares your changes for commit but doesn't create a commit yet.

    • You can stage individual files or specific changes within files using the git add command. For example, to stage all changes in a file:

        git add path/to/your/file
      

  4. Review Staged Changes:

    • After staging changes, you can use the git diff --staged command to see the differences between your staged changes and the previous commit.

  5. Create a Commit:

    • Once you've staged your changes, use the git commit command to create a commit. A commit records the changes you've staged along with a commit message describing the purpose of the changes.

    • The commit message should be clear and concise, explaining the why and what of the changes. For example:

        git commit -m "Added new feature X"
      

  6. Repeat for Additional Changes:

    • Continue making changes, staging them, and creating commits as needed. Each commit should represent a logical unit of work.
  7. View Commit History:

    • You can use the git log command to view the commit history of your repository. This shows a list of commits along with their commit messages, authors, timestamps, and unique commit hashes.