Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Instructions

  1. Read Chapters 2 & 3 of Pro Git. The chapters are short.
  2. Answer these questions using Markdown format (also Github Markdown).
  3. 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 "$"
    
  4. 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
    
  5. Test that your answers are correct! There is no excuse for incorrect answers since you can test your answers by experimentation.
  6. 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.

Using Git

Basics
Adding and Changing Things
Undo Changes and Recover Files
Viewing Commits
Branch and Merge
Commands for Remotes
Favorites
Resources

Note on Paths

In this file, directory paths are written with a forward slash as on MacOS, Linux, and the Windows-Bash shell: /dir1/dir2/somefile.

Basics

  1. When using Git locally, what are these? Define each one in a sentence

    • Staging area - Is an intermediate step between your working copy and the committed state.
    • Working copy - The directory on your local machine where you make changes to your files that you can edit and modify.
    • master - Main branch. The default branch in Git.
    • HEAD - Represents your current position within the commit history.
  2. 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"
    
  3. There are 2 ways to create a local Git repository. Briefly descibe each one:

    • Initializing a New Repository: Use the command 'git init' to inform Git about your local directory
    • Cloning from a Remote Repository: Utilize the command 'git clone' to replicate a repository from the server to your local machine.

Adding and Changing Things

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
    ...
  1. Add README.md and everything in the src directory to the git staging area.

    git add README.md
    git add src/
    
  2. Add test/test_a.py to the staging area (but not any other files).

    git add test/test_a.py
    
  3. List the names of files in the staging area.

    git diff --staged --name-only
    
  4. Remove README.md from the staging area. This is very useful if you accidentally add something you don't want to commit.

    git reset HEAD README.md
    
  5. Commit everything in the staging area to the repository.

    git commit
    
  6. 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:

    • IDE and editor files: '.vscode/', '.idea' etc.
    • Logs and Temporary Files: '*.log' , '*.tmp'
    • Python cache: *.pyc , *.pyo , __pycache__/
  7. Command to move all the .py files from the src dir 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 .
    
  8. In this repository, create your own .gitignore file 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.

Undo Changes and Recover Files

  1. Display the differences between your working copy of a.py and the a.py in the local repository (HEAD revision):

    git diff a.py
    
  2. Display the differences between your working copy of a.py and 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
    
  3. 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 b.py
    
  4. Undo "git add": If main.py has been added to the staging area (git add main.py), remove it from the staging area:

    git reset main.py
    
  5. Recover a file: Command to replace your working copy of a.py with the most recent (HEAD) version in the repository. This also works if you have deleted your working copy of this file.

    git restore a.py
    
  6. 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
    
  7. Checkout old code: Using the above example, the command to replace your working copy with the files from commit with id aaaa:

    git checkout aaaa
    

    Note:

    • 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.

Viewing Commits

  1. Show the history of commits, using one line per commit:

    git log --oneline
    

    Some versions of git have an alias "log1" for this (git log1).

  2. Show the history (as above) including all branches in the repository and include a graph connecting the commits:

    git log --oneline --decorate --graph --all
    
  3. List all the files in the current branch of the repository:

    git ls-tree --name-only HEAD
    

    Example output:

    .gitignore
    README.md
    a.py
    b.py
    test/test_a.py
    test/test_b.py
    

Branch and Merge

  1. To display a list of all branches in a Git repository:
    git branch
    
  2. Show current branch:
    git branch --show-current
    
  3. Create a new branch:
    git branch new-branch
    
  4. Switch to another branch:
    git checkout target-branch
    
  5. Create a new branch and switch to it:
    git checkout -b new-branch
    
  6. Git merge (make sure you are currently in master/main):
    git merge branch-name
    

Favorites

My favorite task it git push, and you can follow step by step:

  1. Check all status of your repository are be commited:
    git status
    
  2. If files are not commited do this (if not skip to 5.):
    git add .
    
    or specific file:
    git add example.py
    
  3. Git commit (you can select one of these):
  4. git commit
    git commit -m "Your text"
    
  5. Add the Remote Repository (If first time):
    git remote add origin <repository-url>
    
  6. Git push: specific branch:
    git push origin branch-name
    
    all branch:
    git push --all origin
    

Resources

my favorite git resource:

Learn Git Visually:

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Generated from ISP2023/git-commands