Important Git Commands
Published: 2022-04-09 19:27:12

```
git status = will show us status.
git log = it shows all commit.
git add . = to add file to the git repository.
git commit -m "commit message"
git branch = to know corresponding branch
git init = it create git file in folders.(initialize)
git checkout -b #branch name# = it creates new branch (-b) and bring us to that branch.
git checkout #branchname# = to switch branch

git checkout filename = it will discard the changes made to the file.(works before add .)
git diff = it shows the changes made to the file.(it works only before add)
git add -u = to add the already added modified file.
git reset #filename# = it removes the added files.(it works only before commit and after add.)
git merge #branch_name# = to move changes made in one branch to another.

git clone #https repo link# = it will take that repo to our local machine folder
fork = to add others githb repo to our githb repo
git push origin master = for pushing to repo

git config user.name "name"
git config user.email "email"
```

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

## Stages in Git file tracking.
- **Modified state**
A file in the modified state has some changes made to it but it's not yet saved. This means that the state of the file has been altered from its previous state in the committed state.
- **Staged state**
A file in the staged state means it is ready to be committed. In this state, all necessary changes have been made so the next step is to move the file to the commit state.
_To change our file from modified state to staged state we must use the command_: `git add .`
- **Committed state**
A file is in the committed state when all the changes made to the file have been saved in the local repo. Files in the committed stage are files ready to be pushed to the remote repo (on GitHub).
_To change our file from staged state to committed state we must use the command_: `git commit -m "first commit"`


The above three, `git init` `git add .` `git commit -m "first commit"` are the three main comments to work with git. Next we are moving to github.

## Push the local repository(in our computer) to the remote repository(Github)
A file can be added to github only after implementing the previous three stages we have done in git above.

**git remote add origin https://github.com/ihechikara/git-and-github-tutorial.git**
The first command git remote add origin https://github.com/ihechikara/git-and-github-tutorial.git creates a connection between your local repo and the remote repo on Github.

**git branch -M main**
The second command git branch -M main changes your main branch's name to "main". The default branch might be created as "master", but "main" is the standard name for this repo now. There is usually no response here.

**git push -u origin main**
The last command git push -u origin main pushes your repo from your local device to GitHub.

## Pushing modified files to GitHub
We first add the file by using `git add .` which adds all the files in the folder (one file in our case). Then we commit the file by running `git commit -m "added new task"` followed by `git push -u origin main`.

## How to use Branches in Git
In Git, branches are a fundamental concept that allows you to work on different versions of your project simultaneously without affecting the main codebase. With branches, you can create a copy of a file you would like to work on without messing up the original copy. 

**Create new branch**
`git checkout -b test`
`checkout` tells Git it is supposed to switch to a new branch. `-b` tells Git to create a new branch. `test` is the name of the branch to be created and switched to.

**To switch between branches**
`git checkout main`
Did you notice that we did not add `-b` ? This is because we are not creating a new branch but rather switching to an existing one. You can check all the branches that exist in your repo by running the `git branch` command.

**Merge branch**
`git merge test`


If you go on to push your repo to GitHub, you'll see that the test branch will not be pushed. It will only remain in your local repo. If you would like to push your test branch, switch to the branch using `git checkout test` and then run `git push -u origin test`.