Course lesson
Add More Files and Changes to a Commit Before Pushing
To add more files to the most recent commit, we can add them to the staging area with:
- Duration
- 2 min
- Access
- Free
- Transcript
- Retained from source evidence
To add more files to the most recent commit, we can add them to the staging area with:
git add -A
and then rewrite the most recent commit to include them with:
git commit --amend -m "My new message"
Caution: only do this BEFORE you've pushed the commits - otherwise you will be re-writing history for people who may have already pulled down the old commits.
We can use git log --oneline to see information about our previous commits like what files were added to certain commits. There is something we can do if we want to add more than one file to the same commit. We can touch app.js and edit it.
app.js
// our app js codeTo see that, we need to add it in the <head> of the index.html in our previous commit. We type <script type="text/javascript" src="/app.js"></script>.
index.html
<html>
<head>
<script type="text/javascript" src="/app.js"></script>
</head>
<body>
<h1>Fixing git mistakes</h1>
</body>
</html> Now that we have our script tag added to our index.html, when we do git status we have one untracked file and one modified file. However, we want to add those to the same commit. To do this, we add both to the stage with git add -A and we have both as a change to be committed. We then git commit --amend -m "Adding index.html and app.js" to add them to the same previous commit. Now, with git status, we have no changes to be committed. A git log --oneline, all files were added to the same commit along with a rewritten commit message.