Using Git, you will want to make changes and commit snapshot of those changes into your repository each time your project reaches a state you want to record. Recall from Status of your files that a each file in your working directory can be in either tracked or untracked status.

Tracking new files

You use git add command to begin tracking new files or untracked files. If you want to track a README.md file, you can run the below command.

git add README.md

If you run your git status command, you can see that your README.md file is now tracked and staged to be committed.

$ git status
On branch master
Your branch is up-to-date with 'origin/main'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
 
    new file:   README.md

You can tell that it’s staged because it’s under the “Changes to be committed” heading.

Staging modified files

If you modified a file that is being tracked by Git called CONTRIBUTING.md and then you run the git status command.

$ git status
On branch main
Your branch is up-to-date with 'origin/main'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
 
    new file:   README.md
 
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
 
    modified:   CONTRIBUTING.md

The CONTRIBUTING.md file appears under the “Changes not staged for commit” heading, which means that a file that is tracked has been modified in the working directory but not yet staged.

Now you run the git add command, it is a multipurpose command, you use it to begin tracking files, to stage files, and to do other things like making merge-conflicted files as resolved.

git add CONTRIBUTING.md 

After you staged the CONTRIBUTING.md, and then run git status again.

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
 
    new file:   README.md
    modified:   CONTRIBUTING.md

Both files are staged and will go into your next commit. At this point of time, if you made any changes to these staged files, you will have to run the git add command again to stage the latest version of the file.

Git provides a more compact output format for the git status command using flag -s or --short.

Ignore files

If you have files directories that you don’t want Git to automatically add or even show you as being untracked, you can define the Git ignore rules in a .gitignore file. The .gitignore file is often defined at the root of your repository. However, you can choose to define multiple .gitignore files in different directories in your repository. Each pattern in a particular .gitignore file is tested relative to the directory containing that file.

The git ignore file uses special patterns matching syntax for ignoring files and directories.

  • Ignoring a specific file
<filename>
  • Ignoring all files with a specific extension
*.<extension>
  • Ignoring all files in a directory
/<directory_name>/
  • Ignoring all files with specific extension in all subdirectories
<directory_name>/*.<extension>
  • Ignoring everything in a directory except one file
/<directory_name>/*
!/<directory_name>/<file_name>
  • Ignoring all files recursively with a specific name
.<file_name>

Here is an example of the .gitignore file.

# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

Committing your changes

Once your staging area is set up the way you want it, you can commit your changes. Anything you that is still unstaged will not go into this commit. You use the git commit command for committing changes.

git commit

Doing so launches your editor of choice. The editor displays the following text (this is an example of a Vim editor screen).

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/main'.
#
# Changes to be committed:
#	new file:   README
#	modified:   CONTRIBUTING.md
#
~
~
~
".git/COMMIT_EDITMSG" 9L, 283C

Here you will entre your commit message. Alternatively, you can type your commit message inline with the commit command by specifying it after a -m flag.

git commit -m "Your commit message here."

Now you’ve created your commit. You will see the commit’s output about itself: which branch you committed to, what SHA-1 checksum the commit has, how many files were changed, and statistics about lines added and removed in the commit.


Back to parent page: Git

DevOpsVCSGitGit_Ignore

Reference: