5 Make changes to files

5.1 Add files

  • git status shows status (which branch we’re on)

  • Create second_file.txt and third_file.txt and then git status.

    • Untracked files: (use "git add <file>..." to include in what will be committed) second_file.txt third_file.txt

  • If changes are made to untracked files, there is no record of those changes. For example, if I change second_file.txt, there is no record.

  • So git add second_file.txt and then git status.

    • Changes to be committed: (use "git restore --staged <file>..." to unstage) new file:   second_file.txt Untracked files: (use "git add <file>..." to include in what will be committed) third_file.txt

  • It’s possible to unstage files. We will instead commit. git commit -m "Adds second file to project"

  • git log shows new commit and SHA value to which HEAD points.

  • git status no longer shows staging area.

  • Next add and commit third_file.txt

  • git status no longer shows untracked files since all files are tracked.

5.2 Edit files

In git, the process of adding and editing files are identical since we’re essentially adding changes.

  • We made changes to first and second file, added and commited them. Both changes were tracked using the same commit/same SHA value.

  • Then we changed the third file, added and commited them. (It’s the same even if the third file was changed before).

5.3 View changes

  • git status tells us there is a change but not what the change is.

  • git diff shows differences in tracked files.

    • It shows all lines that were changed.

  • Workflow should include this so we can make a decision on whether o r not we want to commit these changes.

5.4 View only staged changes

  • If a file is added to the staging index, git diff no longer tells us about changes to that file.

    • git diff by defauly compares working directory to staging tree

  • git diff --staged shows changes between repository and staging

  • git diff --cached and --staged are aliases. --staged is encouraged

5.5 Delete files

  • How do we track files when they’re deleted?

  • Add file_to_delete1.txt and file_to_delete2.txt. add and commit.

  • Technique 1: delete the file manually.

    • git status: deleted:    file_to_delete1.txt

    • to commit this change to the repository, how do we add this change?

    • git rm file_to_delete1.txt and commit. This cleans the working tree.

  • Technique 2: we can instead tell git to remove it

    • git rm file_to_delete2.txt

    • no longer in trash, uses a linux/unix command to remove permanently.

    • if we need it, we can get a copy from the repository

    • already adds to staging area/directory

    • git commit to clean the working tree

5.6 Move and rename files

  • Technique 1: manually rename the file in a directory

    • change first_file.txt to primary_file.txt

    • git status shows first_file.txt deleted and primary_file.txt untracked

    • git add and git rm to stage these changes

    • git status now recognized renaming of the files

  • Technique 2: moving and renaming are the same.

    • git mv second_file.txt secondary_file.txt

    • renames and adds changes to staging area in one command.

  • suppose you manually move third_file.txt to folder first_folder, git status shows deletion and untracked file

    • instead git mv third_file.txt first_folder/third_file.txt

    • git status again recognizes this as renaming