Git
Published: 2024-07-01 08:25:46

## I. Add Commits to A Repo
- Configuration
```$ git --version```

```
$ git config --global user.name "<NAME>"   
$ git config --global user.email "<EMAIL>"
$ git config --global color.ui auto
$ git config --global merge.conflictstyle diff3
$ git config --global core.editor "code --wait"
```
- Check configuration 
  ```$ git config --list```

- To make a commit, the file or files we want committed need to be on the Staging Index. Command do we use to move files from the Working Directory to the Staging Index
```$ git add```

- Command takes files from the Staging Index and saves them in the repository
```$ git commit```

- Bypass The Editor With The -m Flag
```$ git commit -m "Initial commit"```

These Changes Were Not Committed on local let use this to know what those changes actually were

```$ git diff```

- Good Commit Messages
- Do
do keep the message short (less than 60-ish characters)
do explain what the commit does (not how or why!)

- Do not
do not explain why the changes are made (more on this below)
do not explain how the changes are made (that's what git log -p is for!)
do not use the word "and"
if you have to use "and", your commit message is probably doing too many changes - break the changes into separate commits
e.g. "make the background color pink and increase the size of the sidebar"

- To explain Why 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtt8nbrf0z4jt0rcg0nr.png)

##II. Tagging, Branching, and Merging
### **Tagging**

```$ git tag -a v1.0```

- Verify tag
```$ git tag```
- Delete tag - A Git tag can be deleted with the -d flag
```$ git tag -d v1.0```
- Adding A Tag To A Past Commit
```$ git tag -a v1.0 a87984```

### **Branch**
- Verify branch 
```git branch```
- Create branch
```git branch sidebar```
- Create Git Branch At Location
```$ git branch alt-sidebar-loc 42a69f```
- Create branch + switch to it right after
```git checkout -b <new_branch_name> <at_SHA or at <name current_branch>```

_Will create the `alt-sidebar-loc` branch and have it point to the commit with SHA `42a69f`_
- Switch to desired branch
```git checkout sidebar```
_How this command works:
- Remove all files and directories from the Working Directory that Git is tracking
(files that Git tracks are stored in the repository, so nothing is lost)
- Go into the repository and pull out all of the files and directories of the commit that the branch points to_

- Show branch in log
```$ git log --oneline --decorate```
- Show all branch in gragh
```git log --oneline --decorate --graph --all```
- Delete branch, need to switch to other branch firstly  (-D force delete)
```$ git branch -d sidebar```
### **Merge**
- There are two types of merges:

Fast-forward merge – the branch being merged in must be ahead of the checked out branch. The checked out branch's pointer will just be moved forward to point to the same commit as the other branch.
the regular type of merge
two divergent branches are combined
a merge commit is created

- Git merge to combine branch (merging some other branch into the current (checked-out) branch)
```$ git merge <name-of-branch-to-merge-in>```
- if you make a merge on the wrong branch, use this command to undo the merge
```$ git reset --hard HEAD^```


### **Undoing Changes**

- Update commit by modifying message or Add Forgotten Files To Commit
1. Make changes required file and do ```git add``` (if any)
2. Update message and commit file via ```$ git commit --amend```

The ```git revert``` command is used to reverse a previously made commit:

```$ git revert <SHA-of-commit-to-revert>```
This command: 
1. Will undo the changes that were made by the provided commit
2. creates a new commit to record the change


### **Reset vs Revert**
_**Resetting** Is Dangerous_

- Reverting creates a new commit that reverts or undos a previous commit.
- Resetting, on the other hand, erases commits! (with flag --mixed (default to working dir); --soft; --hard)

However, Git does keep track of everything for about 30 days before it completely erases anything by ```git reflog``` command

💡 Create a backup branch on the most-recent commit so that I can get back to the commits if I make a mistake:

```$ git branch backup```

##III. Working with remote
A remote repository is a repository that's just like the one you're using but it's just stored at a different location. To manage a remote repository, use the git remote command:

```$ git remote```
It's possible to have links to multiple different remote repositories.
A shortname is the name that's used to refer to a remote repository's location. Typically the location is a URL, but it could be a file path on the same computer.
git remote add is used to add a connection to a new remote repository.
git remote -v is used to see the details about a connection to a remote.

- Git push (sync the remote repository with the local repositor)
```$ git push <remote-shortname> <branch>```
```$ git push origin master```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d5b4uoxf51rm4fx7o77.png)

You'd like to include in your local repository, then you want to pull in those changes
```$ git pull origin master```

When you want to use ```git fetch``` rather than git pull is if your remote branch and your local branch both have changes that neither of the other ones has. In this case, you want to fetch the remote changes to get them in your local branch and then perform a merge manually. Then you can push that new merge commit back to the remote.

```$ git fetch origin master```

### **Working On Another Developer's Repository**
- Git log
```$ git shortlog -s -n```
```$ git log --author=Surma``` (tìm gần đúng tên)
```$ git log --author="Surma Lewis"``` (tìm chính xác tên)

```git log --grep=bug``` (Tìm text có chữ bug)
```git log --grep="this bug"``` (Tìm text có chữ this bug)

## NOTE
Before you start doing any work, make sure to look for the project's CONTRIBUTING.md file.

Next, it's a good idea to look at the GitHub issues for the project

look at the existing issues to see if one is similar to the change you want to contribute
if necessary create a new issue
communicate the changes you'd like to make to the project maintainer in the issue
When you start developing, commit all of your work on a topic branch:

do not work on the master branch
make sure to give the topic branch clear, descriptive name
As a general best practice for writing commits:

make frequent, smaller commits
use clear and descriptive commit messages
update the README file, if necessary

## Stay Syncing with source
When working with a project that you've forked. The original project's maintainer will continue adding changes to their project. You'll want to keep your fork of their project in sync with theirs so that you can include any changes they make.

To get commits from a source repository into your forked repository on GitHub you need to:

get the cloneable URL of the source repository
create a new remote with the git remote add command
use the shortname upstream to point to the source repository
provide the URL of the source repository
fetch the new upstream remote
merge the upstream's branch into a local branch
push the newly updated local branch to your origin repo


## GIT standard
Commit Style Requirements {% embed https://udacity.github.io/git-styleguide/ %}