- Read Chapters 2 & 3 of Pro Git. The chapters are short.
- Answer these questions using Markdown format (also Github Markdown).
- Place your answers between lines beginning with 3 backquotes, which tells Markdown it should be unformatted text, and write only the commands you would type (no shell prompt). E.g.:
git status CORRECT $ git status WRONG - you do not type "$" - Indent the 3 backquotes so they line up with the question text (3 leading spaces) so Markdown formats you answer as part of the numbered item.
Example:
git init - Test that your answers are correct! There is no excuse for incorrect answers since you can test your answers by experimentation.
- Verify that your Markdown formatting is correct -- points deducted for bad formatting. VS Code and IntelliJ have markdown previewers. You should also preview it on Github, since Github Markdown is a bit non-standard.
TODO: Delete these instructions before you submit your work. Points deducted for each "TODO" in this file.
Basics
Adding and Changing Things
Undo Changes and Recover Files
Viewing Commits
Branch and Merge
Commands for Remotes
Favorites
Resources
In this file, directory paths are written with a forward slash as on MacOS, Linux, and the Windows-Bash shell: /dir1/dir2/somefile.
-
When using Git locally, what are these? Define each one in a sentence
- Staging area - It's where you prepare changes before they're committed to the repository.
- Working copy - It's essentially the set of files and directories in your local repository that you can directly edit.
- master - The default and primary branch in a Git repository. It's often used as the main development branch where stable code resides
- HEAD - A reference that points to the latest commit in the currently checked-out branch. It represents your current position in the Git history.
-
When you install git on a new machine (or in a new user account) you should perform these 2 git commands to tell git your name and email. These values are used in commits that you make:
# Git configuration commands for a new account git config --global user.name "name" git config --global user.email "email" -
There are 2 ways to create a local Git repository. Briefly descibe each one:
-
git initis a command used in Git to initialize a new repository in a directory. When you run this command in a specific directory, Git sets up the necessary data structures and files to start tracking changes to your project's files. This creates a new Git repository from scratch, allowing you to version control your project and utilize Git's features such as version tracking, branching, and collaboration. It will create .git sub folder in your local directory -
git cloneis a commant used to create a copy of an existing Git repository. It allows you to duplicate an entire repository, including all its history, branches, and files, onto your local machine or another server.
-
Suppose your working copy of a repository contains these files and directories:
README.md
out/
a.exe
src/a.py
b.py
c.py
test/
test_a.py
...
-
Add README.md and everything in the
srcdirectory to the git staging area.git add README.md src/ -
Add
test/test_a.pyto the staging area (but not any other files).git add test/test_a.py -
List the names of files in the staging area.
git status -
Remove
README.mdfrom the staging area. This is very useful if you accidentally add something you don't want to commit.git reset README.md -
Commit everything in the staging area to the repository.
git commit -m "Finish" -
In any project, there are some files and directories that you should not commit to git.
For a Python project, name at least files or directories that you should not commit to git:- pycache
- .log
- venv
-
Command to move all the .py files from the
srcdir to the top-level directory of this repository. This command moves them in your working copy and in the git repo (when you commit the change):git mv src/*.py ./ -
In this repository, create your own
.gitignorefile that you can reuse in other Python projects. Add everything that you think is relevant.
Hint: A good place to start is to create a new repo on Github and during the creation dialog, ask Github to make a .gitignore for Python projects. Then edit it. Don't forget to include pytest output and MacOS junk.
- Display the differences between your working copy of
a.pyand thea.pyin the local repository (HEAD revision):
git diff a.py
- Display the differences between your working copy of
a.pyand the version in the staging area. (But, if a.py is not in the staging area this will compare working copy to HEAD revision):
git diff --staged a.py
- View changes to be committed: Display the differences between files in the staging area and the versions in the repository. (You can also specify a file name to compare just one file.)
git diff --staged
- Undo "git add": If
main.pyhas been added to the staging area (git add main.py), remove it from the staging area:
git reset main.py
- Recover a file: Command to replace your working copy of
a.pywith the most recent (HEAD) version in the repository. This also works if you have deleted your working copy of this file.
git checkout a.py
-
Undo a commit: Suppose you want to discard some commit(s) and move both HEAD and "master" to an earlier revision (an earlier commit) Suppose the git commit graph looks like this (
aaaa, etc, are the commit ids)aaaa ---> bbbb ---> cccc ---> dddd [HEAD -> master]The command to reset HEAD and master to the commit id
bbbb:git reset --hard bbbb -
Checkout old code: Using the above example, the command to replace your working copy with the files from commit with id
aaaa:git checkout aaaaNote:
- Git won't let you do this if you have uncommitted changes to any "tracked" files.
- Untracked files are ignored, so after doing this command they will still be in your working copy.
-
Show the history of commits, using one line per commit:
git log --onelineSome versions of git have an alias "log1" for this (
git log1). -
Show the history (as above) including all branches in the repository and include a graph connecting the commits:
git log --graph --all --decorate --oneline -
List all the files in the current branch of the repository:
git ls-tree -r HEAD --name-onlyExample output:
.gitignore README.md a.py b.py test/test_a.py test/test_b.py
- Create a New Branch: To create a new branch in Git, you can use the following command:
git branch dev
- witch to a Different Branch: To switch to a different branch, you can use the git checkout command:
git checkout dev
- Merge Branches: To merge changes from one branch into another, you can use the git merge command:
git checkout master
git merge dev
- Delete a Branch: To delete a branch after you've finished with it, you can use the -d flag with git branch:
git branch -d dev
Push Changes to a Remote Repository: To push your local commits to a remote repository, such as on GitHub or GitLab, use the git push command:
git push origin master
-
redit git resources summary many website and recomment git command for each situation.
-
Pro Git Online Book Chapters 2 & 3 contain the essentials. Downloadable e-book is available, too.
-
Visual Git Reference one page with illustrations of git commands.
-
Markdown Cheatsheet summary of Markdown commands.
-
Github Markdown some differences in the way Github handles markdown and special Markdown for repos.
Learn Git Visually:
- Learn Git Interactive Tutorial great visual tutorial
- Git Visualizer execute Git commands in a web browser and see the results as a graph.