Git Commands used on my workflow
Tags: beginners, git, productivity
Published: 2022-01-31 18:51:06

## What is Git?

Git is the most widely used versioning tool today. Git is an open-source project created in 2005 by Linus Torvalds, the same creator of the Linux kernel.

## How does git work?

Git has a distributed architecture where every copy of the code is also a repository with a complete history of all changes. If the online repository has been deleted, there will still be a faithful copy on your machine.

## Terminology

**HEAD**: The term is used to refer to the last change committed on your branch.

**Repo**: Abbreviation for repository

## Branches

When a repository is created the main branch is also created. From the initial branch, it is possible to create branches of your code and then merge them into the main branch.
The main branch used to be called master branch, but it has changed on most common online repositories because it's a racist term that comes from master/slaves.

```
┐►Main
│
└┐►Release
 │
 └┐►Develop
  │
  └─►Feature
```
The image above represents what we call ["Git flow"](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow), it's a way to separate branches to develop as a team, with branches representing a stage of development, feature being the new feature that is being implemented, the development being the development environment for testing, release being the homologation and the main being the production repository

## Git Ignore

Git ignore is represented as a file called ".gitignore", as the name implies it is made to ignore files and/or directories. All documents with the same name written in the file will be ignored by git versioning.
There are sites to generate the gitignore file such as [gitignore.io](gitignore.io)

```
# Git ignore para vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide
```
The git ignore file is very important when you are using something like node with npm because all the libs that are used in the project are saved in the same directory as your project, so when you push it to the remote repo it's possible to push many gigs of data that you cloud download with `npm install`.

## Git Commands
Nowadays many interfaces allow you to use git without writing a single command, but it's important to know what is happening from behind the scenes, that's why I'm bringing the main git commands.


### Main Commands
These are the main basic commands everyone must know when using git. Most of these commands you are going to use every day for the rest of your developer life.
```bash
# Clone a repository to your machine
git clone <url>
# Add files to be committed 
git add <arquivo>
# Commit files and add a message
git commit -m <message>
# Show the repository status
git status
# Pull the changes from a remote repository
git pull origin <branch>
# Push the changes to a remote repository
git push origin <branch>
# Initiate a repository
git init
```
### Configuration
The configuration commands normally are used only once when you set up your machine, but it's handy to have them written somewhere because it's very easy to forget.
```bash
# The --global flag makes the config
# been applied to the whole machine

# These commands must be set up
# before you first commit

# Add your email to commit Log
git config --global user.email {email}
# Add your name to a commit log
Git config --global user.name {name}

```
### Branches Commands

The following commands are used to interact with branches.

```bash
# Create a new branch
git branch <name>
# Lists all branches local and remote
git branch -a
# Delete a branch
git branch -d <name>
# Changes from current branch to another
git checkout <branch>
# creates a new branch from current and
# changes to it.
git checkout -b <branch>
# merge two branches
git merge <branch>
```
It's good to remember that before you merge a remote repo to your branch always run the `git fetch` command to fetch the remote changes to your local repo.

## Advanced commands
The advance commands are commands that make your life easier by changing history or saving changes on a stack. I normally use one of those commands once a week.

### Git Stash
The stash command saves your uncommitted changes to a stack where you can retrieve them when needed.

```bash
# Save changes when they are not committed
git stash
# Save changes with a reference name
git stash save <name>
# List the references
git stash list
# Remove the last change from the stack and 
# add back to the code
git stash pop
# Add the last change to the code and
# live it on the stack
git stash apply
# Deletes the last change from the stack
git stash drop
```
### Git commit
Back to commit commands now we have more advanced ones that can help us solve some small problems

```bash
# Add and commit at the same time
git commit -am <message>
# Changes the message from the last commit
git commit --amend -m <message>
# Add new changes to the commit
# To push the change to the remote repo
# you may need to use `--force`
git commit --amend --no-edit
# Revert the last change without deleting the logs
git revert <hash>
# Show the logs 
git log
```
## Git Reset
Reset commands are very useful when you need to delete all changes you have done in your code or remove some commits without deleting the changed code

```bash
# Remove the commit and keep the changes 
git reset --soft HEAD~<commmit_number>
# Remove the commit delinting the changes
git reset --hard HEAD~<commit_number>
# Deletes the changes and go back to HEAD
git reset --hard HEAD
```

> We always have to remember that git reset is a command that "rewrite history" which means that all you commit history will be going to be affected by the commanding. Be careful before using it.

## Git Cherrypick
This is my favorite command of all. It allows you to copy a commit from one branch to another. It's very useful when you want to deploy some code but your release branch is dirty, you can simply copy your commit to the main branch and deploy it.

```bash
# Copy a commit to your current branch
git cherry-pick <hash>
```