Understanding Git Commits and Repositories
Git Commits
In Git, a commit is a snapshot of your project at a specific point in time. Each commit includes the changes you've made since the last commit, as well as metadata such as the author, date, and a commit message.
To create a new commit, you first need to stage the changes you want to include using the git add
command. Then, you can create the commit using the git commit
command, like this:
## Stage the changes
git add file1.txt file2.txt
## Create a new commit
git commit -m "Add new features"
Each commit has a unique identifier, called a commit hash, which is a long string of letters and numbers that represents the state of the repository at that point in time.
Git Repositories
A Git repository is a directory that contains all the files and folders of your project, along with their complete history. Git repositories can be either local, stored on your own computer, or remote, hosted on a platform like GitHub, GitLab, or Bitbucket.
To create a new Git repository, you can use the git init
command:
## Create a new Git repository
git init my-project
Once you have a Git repository, you can start tracking changes to your files using the git add
and git commit
commands.
Viewing Git History
You can view the commit history of your repository using the git log
command. This will show you a list of all the commits, including their commit hashes, author information, and commit messages.
## View the commit history
git log
You can also use the git show
command to view the changes introduced by a specific commit:
## View the changes in a specific commit
git show 1234567890abcdef
By understanding how Git commits and repositories work, you'll be better equipped to manage your project's version history and collaborate with others.