Command | Notes |
---|---|
git init | Initializes a local git repo |
git status | Shows the working tree status. Shows files that are staged for commit (are in the index), as well as files that you could commit if you first ran git add. Those changes exist in the working directory, but not in the index. |
git add | Adds the files to the index - stages the changes |
git commit | Commits changes in the index to the respository |
git log | Shows the git commit logs�� --oneline - shows the sha and the commit comments, one line per commit |
git diff | Shows changes between commits, commits and index, index and working directory ��git diff --cached - shows changes between the index and the repository �git diff - shows changes between the working area and the index |
git checkout | Can be used to switch branches or restore working tree files� git checkout HEAD <file> - Because you are already on HEAD, this version of the command will update the index and working area with the version of the file that is in the repository. |
git reset | Moves the current branch to a different commit. If you do not pass a sha for another commit, the command will, in effect, not move the current branch --soft - does not update the files in the working area or the index with the files in the repository --mixed - copies the file(s) in the repository into the index. It does not update the working directory. --hard would update the working directory and index, however, you cannot pass a path (like chapter.md). --hard only works on all changes. |
Learning |
---|
There are 4 main areas in git: - Working directory: This is where you work with (add, edit, delete) files - Index: A staging area where you stage changes before committing them to the Repository - Repository: Stores all the objects - Stash: A place to temporarily store changes |
Objective | Command | Notes |
---|---|---|
Add/Commit | ||
Create the test files by running the setup script | ./setup.sh | This will initialize a git repo, create index.md at the root and a chapters directory with one file, chapter1.md |
cd into the kata directory | cd kata | |
See the current git status | git status | Shows that there are untracked files. This means that they are in the Working Directory, but not the Index or the Repo |
Stage the new files | git add * | Add the files to the index |
Commit the changes | git commit -m "Initial Commit" | Commit the changes to the repo |
Show the git log | git log | This shows the single commit |
Show that the working directory and the index are the same | git diff | The working area and the index are the same |
Show that the index and the repository are the same | git diff --cached | The index and the repository are the same |
Make changes to the index.md file. Add a heading to the top of the file such as # Chapter List and save it. | vi index.md | You can use any editor you like to edit the file, not just vi |
Show that the difference between the working directory and the index | git diff | The working area is different than the index. The working area has the additions you made. My diff looks like this: diff --git a/index.md b/index.md index e031062..4a95f74 100644 --- a/index.md +++ b/index.md @@ -1 +1,2 @@ +# Chapters [Chapter 1](chapters/chapter1.md) |
Show that the index and the repository are still the same | git diff --cached | The index and the repository are the same |
Show the git status | git status | We have an untracked file - this means that it is not yet in the index (staging area) |
Add the updated file into the staging area | git add index.md | This adds index.md to the index |
Show that the working directory and the index are the same | git diff | The working area and the index are the same |
Show that the index has changes that are not yet in the repository | git diff --cached | The index and the repository are different. The index has changes that are staged, but not yet committed. |
Commit the staged changes | git commit -m "Added a header" | This updates the repo with the changes in the index |
Show that the working directory and the index are the same | git diff | The working area and the index are the same |
Show that the index and the repository are the same | git diff --cached | The index and the repository are the same |
Revert Changes That Exist Only in the Working Directory | ||
Make changes to the index.md file. Add a description beneath the # Chapters heading: The following are the chapters: | vi index.md | You can use any editor you would like: Notepad, VSCode, etc. |
Show the git status | git status | This shows that there is a change that has not been staged (not in the index). On branch master Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.md no changes added to commit (use "git add" and/or "git commit -a") |
Throw away the change you just made to the file in the working directory | git checkout HEAD index.md | This command will overwrite the index and working directory with the index.md that is in the repository. |
Show the git status | git status | There are no longer changes in the working directory. On branch master nothing to commit, working tree clean |
Revert Index/Staging Area Changes - Leave Changes in Working Area | ||
Make changes to the index.md file again (the same changes are fine). | vi index.md | You can use any editor you would like: Notepad, VSCode, etc. |
Add the change to the index | git add index.md | Add the changed index.md to the index |
Show the git status | git status | This shows the one change to be committed (index.md is in the index) On branch master Changes to be committed: (use "git reset HEAD ..." to unstage) modified: index.md |
Unstage index.md, but leave the changes in the working directory | git reset index.md | git reset uses the --mixed option by default. --mixed copies the file(s) in the repository into the index. It does not update the working directory. --hard would update the working directory, however, you cannot pass a path (like index.md). --hard only works on all changes. |
Show the git status | git status | This shows that there is one change that is not staged for commit. In other words, the changes are in the working directory and not in the index. On branch master Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.md |
Show the difference between the working directory and the index | git diff | This shows the difference between what is in the Working directory (the updated index.md) and the index. |
Revert Index/Staging Area Changes - Revert Changes in Working Area | ||
Add the change to the index again | git add index.md | Add the changed index.md to the index |
Show the git status | git status | This shows the one change to be committed (index.md is in the index) |
Unstage index.md and remove the changes from the working directory | git reset --hard or git checkout HEAD index.md |
Git reset --hard does not allow you to pass in a path. It will reset the entire index and working directory with the contents of the repository You can use git checkout HEAD and pass the path |
Show the git status | git status | This shows that the working directory, the index and the repository are all the same. |
Revert Changes That Were Committed / Revert to an Earlier Commit | ||
Make changes to the index.md file again (the same changes are fine). | vi index.md | You can use any editor you would like: Notepad, VSCode, etc. |
Add the change to the index | git add index.md | Add the changed index.md to the index |
Commit the staged changes | git commit -m "Added a description to the index page" | This updates the repo with the changes in the index |
Show the git log - just 1 line per commit | git log --oneline | The following are the results: 1279007 (HEAD -> master) Added a description to the index page 47f0462 Added a header d3d1229 Initial Commit |
Throw away the changes from the last commit (Added a description to the ndex page) in the repository. However, keep the changes in the index and the working directory. | git reset --soft <commit sha to reset to> | git reset --soft 47f0462. This will move the branch pointer to the referenced commit (Added a header). |
Show the git log - just 1 line per commit | git log --oneline | Results: 47f0462 (HEAD -> master) Added a header d3d1229 Initial Commit |
Show the git status | git status | This shows that there is a change to index.md that is staged to commit. This means that the changes to the working directory and index were not overwritten. |
Throw away the changes in the repository and index | git checkout HEAD index.md | This will overwrite index.md in the index and the repository with the index.md in the repository |
Show the git status | git status | This shows that there are not changes. The working directory, the index and the repository are all the same. |