354 lines
13 KiB
Markdown
354 lines
13 KiB
Markdown
|
||
---
|
||
|
||
To enhance your Git skills, here's a breakdown of the commands you already know and some additional commands and practices that can help you work more efficiently:
|
||
|
||
### Basic Git Workflow
|
||
|
||
1. **Check Status:**
|
||
```sh
|
||
git status
|
||
```
|
||
This shows the current state of your working directory and staging area.
|
||
|
||
2. **Add Changes:**
|
||
```sh
|
||
git add .
|
||
git add file_name
|
||
```
|
||
The first command stages all changes, while the second stages specific files.
|
||
|
||
3. **Commit Changes:**
|
||
```sh
|
||
git commit -m "Your commit message"
|
||
```
|
||
This records the staged changes in the repository with a message.
|
||
|
||
4. **Push Changes:**
|
||
```sh
|
||
git push origin main
|
||
```
|
||
This uploads your local commits to the remote repository.
|
||
|
||
### Advanced Git Commands and Practices
|
||
|
||
1. **Interactive Add:**
|
||
```sh
|
||
git add -p
|
||
```
|
||
This allows you to interactively review and stage changes hunk by hunk.
|
||
|
||
2. **Restore Changes:**
|
||
```sh
|
||
git restore <file>
|
||
```
|
||
This discards changes in the working directory.
|
||
|
||
3. **Remove Files:**
|
||
```sh
|
||
git rm <file>
|
||
```
|
||
This stages the removal of files from the repository.
|
||
|
||
4. **Check Logs:**
|
||
```sh
|
||
git log
|
||
git log --oneline --graph --decorate --all
|
||
```
|
||
These commands show commit history. The second one provides a more visual representation.
|
||
|
||
5. **Branching and Merging:**
|
||
```sh
|
||
git branch
|
||
git branch new-branch
|
||
git checkout new-branch
|
||
git checkout -b new-branch
|
||
git merge new-branch
|
||
```
|
||
These commands manage branches, allowing you to create, switch, and merge branches.
|
||
|
||
6. **Rebase:**
|
||
```sh
|
||
git rebase main
|
||
```
|
||
This integrates changes from another branch into your current branch, maintaining a linear history.
|
||
|
||
7. **Stashing:**
|
||
```sh
|
||
git stash
|
||
git stash apply
|
||
git stash list
|
||
```
|
||
These commands temporarily save changes that aren’t ready to be committed.
|
||
|
||
8. **Resetting:**
|
||
```sh
|
||
git reset --soft HEAD~1
|
||
git reset --hard HEAD~1
|
||
```
|
||
These commands undo changes by resetting to a previous commit. The first keeps changes in the working directory, while the second discards them.
|
||
|
||
9. **Tagging:**
|
||
```sh
|
||
git tag v1.0
|
||
git push origin v1.0
|
||
```
|
||
These commands create and push tags for marking specific points in history.
|
||
|
||
10. **Reverting:**
|
||
```sh
|
||
git revert <commit>
|
||
```
|
||
This creates a new commit that undoes the changes of a specific commit.
|
||
|
||
### Best Practices
|
||
|
||
1. **Write Meaningful Commit Messages:**
|
||
- Use imperative mood: "Fix bug" instead of "Fixed bug".
|
||
- Separate subject from body with a blank line.
|
||
- Limit the subject line to 50 characters.
|
||
|
||
2. **Use Branches for Features and Bug Fixes:**
|
||
- Create separate branches for different features or fixes to keep your main branch clean and stable.
|
||
|
||
3. **Regularly Pull and Rebase:**
|
||
- Keep your branch up to date with the main branch by regularly pulling and rebasing.
|
||
|
||
4. **Review Changes Before Committing:**
|
||
- Use `git diff` to review changes before staging and committing them.
|
||
|
||
By incorporating these advanced commands and best practices into your workflow, you'll become more proficient with Git and improve your version control practices.
|
||
|
||
---
|
||
|
||
### Detailed Explanations and Examples
|
||
|
||
#### 1. **Checking Status:**
|
||
```sh
|
||
git status
|
||
```
|
||
- Shows the current state of the working directory and staging area.
|
||
- Use it frequently to keep track of changes.
|
||
|
||
#### 2. **Adding Changes:**
|
||
```sh
|
||
git add .
|
||
```
|
||
- Stages all changes in the working directory.
|
||
```sh
|
||
git add file_name
|
||
```
|
||
- Stages specific files.
|
||
|
||
#### 3. **Committing Changes:**
|
||
```sh
|
||
git commit -m "Your commit message"
|
||
```
|
||
- Records the staged changes with a message.
|
||
- Use clear, descriptive messages. For example, "Fix typo in README.md".
|
||
|
||
#### 4. **Pushing Changes:**
|
||
```sh
|
||
git push origin main
|
||
```
|
||
- Uploads local commits to the remote repository.
|
||
- Ensure your local branch is up to date with `git pull` before pushing.
|
||
|
||
### Advanced Git Commands and Practices
|
||
|
||
#### 1. **Interactive Add:**
|
||
```sh
|
||
git add -p
|
||
```
|
||
- Interactively review and stage changes hunk by hunk.
|
||
- Useful for staging specific parts of a file.
|
||
|
||
#### 2. **Restoring Changes:**
|
||
```sh
|
||
git restore <file>
|
||
```
|
||
- Discards changes in the working directory.
|
||
- For example, `git restore README.md` will revert changes to the file.
|
||
|
||
#### 3. **Removing Files:**
|
||
```sh
|
||
git rm <file>
|
||
```
|
||
- Stages the removal of files from the repository.
|
||
- For example, `git rm old_file.txt` will remove and stage `old_file.txt` for deletion.
|
||
|
||
#### 4. **Checking Logs:**
|
||
```sh
|
||
git log
|
||
```
|
||
- Shows commit history.
|
||
```sh
|
||
git log --oneline --graph --decorate --all
|
||
```
|
||
- Provides a visual representation of commit history.
|
||
|
||
#### 5. **Branching and Merging:**
|
||
```sh
|
||
git branch
|
||
```
|
||
- Lists all branches.
|
||
```sh
|
||
git branch new-branch
|
||
```
|
||
- Creates a new branch.
|
||
```sh
|
||
git checkout new-branch
|
||
```
|
||
- Switches to the specified branch.
|
||
```sh
|
||
git checkout -b new-branch
|
||
```
|
||
- Creates and switches to a new branch.
|
||
```sh
|
||
git merge new-branch
|
||
```
|
||
- Merges the specified branch into the current branch.
|
||
|
||
#### 6. **Rebasing:**
|
||
```sh
|
||
git rebase main
|
||
```
|
||
- Integrates changes from another branch, maintaining a linear history.
|
||
- Use `git pull --rebase` to keep your branch updated.
|
||
|
||
#### 7. **Stashing:**
|
||
```sh
|
||
git stash
|
||
```
|
||
- Temporarily saves changes.
|
||
```sh
|
||
git stash apply
|
||
```
|
||
- Applies stashed changes.
|
||
```sh
|
||
git stash list
|
||
```
|
||
- Lists all stashes.
|
||
|
||
#### 8. **Resetting:**
|
||
```sh
|
||
git reset --soft HEAD~1
|
||
```
|
||
- Moves HEAD to the previous commit, keeping changes in the working directory.
|
||
```sh
|
||
git reset --hard HEAD~1
|
||
```
|
||
- Moves HEAD to the previous commit and discards changes.
|
||
|
||
#### 9. **Tagging:**
|
||
```sh
|
||
git tag v1.0
|
||
```
|
||
- Creates a tag.
|
||
```sh
|
||
git push origin v1.0
|
||
```
|
||
- Pushes the tag to the remote repository.
|
||
|
||
#### 10. **Reverting:**
|
||
```sh
|
||
git revert <commit>
|
||
```
|
||
- Creates a new commit that undoes changes of a specific commit.
|
||
- For example, `git revert abc1234` will undo changes from commit `abc1234`.
|
||
|
||
### Best Practices
|
||
|
||
1. **Write Meaningful Commit Messages:**
|
||
- Use the imperative mood: "Fix bug" instead of "Fixed bug".
|
||
- Separate subject from body with a blank line.
|
||
- Limit the subject line to 50 characters.
|
||
|
||
2. **Use Branches for Features and Bug Fixes:**
|
||
- Keep your main branch clean and stable.
|
||
|
||
3. **Regularly Pull and Rebase:**
|
||
- Keep your branch up to date with the main branch by regularly pulling and rebasing.
|
||
|
||
4. **Review Changes Before Committing:**
|
||
- Use `git diff` to review changes before staging and committing them.
|
||
|
||
### Additional Tools and Resources
|
||
|
||
1. **Graphical Interfaces:**
|
||
- Tools like GitKraken, Sourcetree, or GitHub Desktop can help visualize changes and manage branches.
|
||
|
||
2. **Learning Resources:**
|
||
- Books: "Pro Git" by Scott Chacon and Ben Straub.
|
||
- Online Courses: GitHub Learning Lab, Codecademy, and Udacity.
|
||
|
||
3. **Automation with Git Hooks:**
|
||
- Automate tasks with Git hooks (e.g., pre-commit, post-commit).
|
||
```sh
|
||
# .git/hooks/pre-commit
|
||
#!/bin/sh
|
||
npm test
|
||
```
|
||
|
||
Implementing these advanced commands and best practices will help you become more proficient with Git, making version control and collaboration smoother and more efficient.
|
||
|
||
---
|
||
|
||
Sure, I'll walk you through your Git configuration based on the `.git/config` file you've shown. This file is specific to your Ansible repository and defines settings and rules that Git will follow for this particular repository. Let's break down each section:
|
||
|
||
### Core Section
|
||
- `repositoryformatversion = 0`: This is the format version of the repository; `0` is the standard for most repositories and doesn't generally need to be changed.
|
||
- `filemode = true`: Tells Git to pay attention to the executable bit of the files, allowing it to track file permission changes.
|
||
- `bare = false`: Indicates that this repository has a working directory where files are checked out. A `true` value would mean this is a bare repository with no working directory, typically used on servers.
|
||
- `logallrefupdates = true`: Enables logging of all reference updates (like branches and tags) made in the repository, which is useful for debugging and understanding the history of references.
|
||
- `ignorecase = true`: Instructs Git to ignore case when checking for file changes, which is important in environments like Windows or macOS, where the filesystem is case-insensitive.
|
||
- `precomposeunicode = true`: This is typically used on macOS to ensure that Git correctly handles filenames with non-ASCII characters.
|
||
|
||
### Remote "origin" Section
|
||
- `url = git@github.com:crazystorm720/ansible.git`: Specifies the URL to the remote repository on GitHub. This is the repository where Git will push to and fetch from. The `git@` indicates you are using SSH for authentication.
|
||
- `fetch = +refs/heads/*:refs/remotes/origin/*`: This line defines the default refspec for fetching. It tells Git to fetch all branches (`refs/heads/*`) from the remote, and store them locally under `refs/remotes/origin/*`. The `+` sign means that Git will update the local references even if it results in a non-fast-forward update.
|
||
|
||
### Branch "main" Section
|
||
- `remote = origin`: This links the `main` branch with the `origin` remote, meaning that operations like `git pull` or `git push` on `main` will default to interacting with `origin`.
|
||
- `merge = refs/heads/main`: Specifies the remote branch that the local `main` branch will be tracking. In this case, it tracks the `main` branch on the remote.
|
||
|
||
### Pull Section
|
||
- `rebase = true`: This setting changes the default behavior of `git pull` to use rebase instead of merging. This means when you `git pull`, Git will rebase your local changes on top of what was fetched, rather than creating a merge commit.
|
||
|
||
Each of these configurations is quite standard and tailored to a typical development workflow, especially in collaborative settings using platforms like GitHub. If you have specific needs or workflows, some of these settings can be adjusted.
|
||
|
||
---
|
||
|
||
The following are the most important files in the .git directory:
|
||
|
||
config: This file contains the Git configuration for the repository. This includes things like the default branch, the remote repositories, and the user's name and email address.
|
||
HEAD: This file contains the SHA-1 hash of the current HEAD of the repository. The HEAD is a pointer to the current commit.
|
||
info/index: This file contains the staging area, which is a list of all of the files that are currently scheduled to be committed.
|
||
objects: This directory contains all of the Git objects in the repository, such as commits, trees, and blobs.
|
||
Highlights:
|
||
|
||
The .git directory contains all of the Git repository data, so it is very important to keep it safe and backed up.
|
||
The config file is the main configuration file for the repository, so it is important to be familiar with its contents.
|
||
The HEAD file contains a pointer to the current commit, so it is important to know how to use it.
|
||
The info/index file contains the staging area, which is a list of all of the files that are currently scheduled to be committed.
|
||
The objects directory contains all of the Git objects in the repository, which are the building blocks of Git commits.
|
||
If you are serious about using Git, it is important to understand the contents of the .git directory and how to use them. There are many resources available online and in books that can help you learn more about Git.
|
||
|
||
To look at your current Git configuration, you can use the following command:
|
||
|
||
git config --list
|
||
This will list all of the Git configuration settings, both global and local.
|
||
|
||
Here are some common Git troubleshooting procedures:
|
||
|
||
If you are having problems with Git, the first thing you should do is check the output of the git status command. This will show you the current state of the repository and any errors that Git has detected.
|
||
If you are having problems pushing or pulling changes, you can try running the git fetch and git push or git pull commands again. You can also try restarting your computer.
|
||
If you are having problems with a specific commit, you can try using the git reset command to undo the commit. You can also try using the git reflog command to find the commit that is causing the problem and then using the git checkout command to revert to that commit.
|
||
Here are some other important items to be aware of when using Git:
|
||
|
||
Git is a distributed version control system, which means that each clone of the repository is a complete copy of the repository. This makes it easy to collaborate with others on the same project.
|
||
Git uses branches to allow you to work on different versions of the code at the same time. You can create a new branch for each feature or bug fix that you are working on.
|
||
Git uses commits to record changes to the repository. Each commit contains a snapshot of the repository at a specific point in time.
|
||
Git uses tags to mark specific commits as important. Tags can be used to mark releases of software or to mark important milestones in a project.
|
||
If you are new to Git, I recommend checking out the Git documentation: https://git-scm.com/doc. It is a great resource for learning more about Git and how to use it.
|