2.5 Git Basics: Adding files and directories, git status

Table of Contents

Git show who the author and committer are?

Git show command can be used to display the author and committer of a commit. To display just the author, use the following command:

git show --pretty=format:"%an" commit-hash

To display just the committer, use the following command:

git show --pretty=format:"%cn" commit-hash

git status

Git status is a command that shows the current state of the repository. It shows which files have been modified, which files are staged for commit, and which files are not tracked by Git.

Different scenarios for adding files to a repository with git add

Git add is used to stage changes for commit. There are different scenarios for adding files to a repository with git add:

  • Adding a single file: To stage changes for a single file, use the following command:
git add file-name
  • Adding all files: To stage changes for all files, use the following command:
git add .
  • Adding files in a directory: To stage changes for all files in a directory, use the following command:
git add directory-name

.gitignore to ignore service files

.gitignore is a file that tells Git which files or directories to ignore. It is useful for ignoring service files or files that are not relevant to the repository.

To create a .gitignore file, create a file with the name .gitignore in the root of the repository and add the files or directories that you want to ignore. For example:

*.log
*.txt
/node_modules

Add empty directories

Git does not track empty directories by default. However, you can add an empty directory to a repository by creating a file inside the directory. For example:

mkdir directory-name
touch directory-name/.gitkeep
git add directory-name/.gitkeep

git commit

Git commit is used to save changes to the repository. It is important to write a descriptive commit message that explains the changes made. To commit changes, use the following command:

git commit

Git commit -m is used to save changes with a commit message. To commit changes with a message, use the following command:

git commit -m "descriptive commit message"

Conclusion

In conclusion, understanding the basics of Git is important for efficient collaboration and version control. Git add stages changes for commit, .gitignore ignores service files, and git commit saves changes to the repository. By using these commands and understanding their functionality, you can effectively manage your repository and collaborate with others.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.