Saving Changes in Git: Committing Your Work
In Git, saving changes involves staging and committing them to your local repository. Here's how to efficiently manage and save your changes:
1. Staging Changes
Before committing, you need to stage your changes. This tells Git which files you want to include in the next commit.
1.1 Stage All Changes
To stage all modified and new files:
git add .
🔹 This stages all changes in your working directory (including new files and modifications).
1.2 Stage a Specific File
To stage a specific file or directory:
git add path/to/your/file.txt
🔹 This stages only the specified file.
2. Committing Changes
After staging your changes, you commit them to record a snapshot of your work.
2.1 Basic Commit
To commit with a message:
git commit -m "Your commit message here"
🔹 Commit message should describe the change (e.g., "Fixed bug in login logic").
2.2 Commit with More Information
If you want to include a detailed commit message:
git commit
🔹 This opens your default text editor where you can write a multi-line commit message.
3. Checking the Status of Your Changes
To check the current state of your working directory and staging area:
git status
🔹 This shows the files that are modified, staged, and untracked.
4. Viewing Commit History
Once you've committed your changes, you can view the commit history:
git log
🔹 This displays all commits in your branch.
🔹 To see a shorter version of the history:
git log --oneline
5. Undoing Changes
If you want to undo changes before committing:
5.1 Unstage a File
To unstage a file (keep changes, but remove from staging):
git reset <file>
5.2 Discard Changes in a File
To discard changes in a file (reset it to the last committed state):
git checkout -- <file>
6. Best Practices
- Write clear commit messages to describe the intent of the change.
- Commit often, but only when you've made a meaningful change to your code.
- Use
git status
regularly to keep track of changes.
Conclusion
Action | Command |
---|---|
Stage all changes | git add . |
Stage a specific file | git add path/to/file |
Commit changes | git commit -m "Message" |
View commit history | git log |
Unstage a file | git reset <file> |
Discard changes in a file | git checkout -- <file> |
🔹 Now your changes are saved! If you need help with anything else, just let me know! 😊