Introduction

Git isdecentralized, distributed version control software that allows a team of developers to work on the same code.

It is called "distributed" because each team member has a complete copy of the code.

Team members can send code to each other, receive it, and develop functionality together and separately from the central server.

Some advantages of using it:

  • It is the current standard.
  • Collaborative, versioned and distributed code.
  • File recovery.
  • Greater control.
  • Shortcuts and plugins .
  • Improve productivity.
Git Flow

Facility:

Web platforms that work with Git:

GIT is not GITHUB
Initial Setup

Setting up Git for the first time

git --version
git config --global user.name "Your Name"
git config --global user.email your@email.com
git config --global user.ui true
git config --global init.defaultBranch main
git config --list
# assigning Visual Studio code as git config editor
git config --global core.editor "code --wait"
git config --global -e
# to standardize line breaks in windows
git config --global core.autocrlf true
# to standardize line breaks on linux/mac
git config --global core.autocrlf input
# view all configuration options in terminal
git config -h
# see all configuration options in the browser
git help config

Initialize Git in a local directory

mkdir folder
cd folder
touch README.md
touch .gitignore
git init
code .
Basic Flow

The Git flow consists of three local states, that is, on the computer where you are working and one more remotely when you access the centralized code on platforms such as GitHub, Gitlab, Bitbucket, etc.

These states are modified, staged, committed, and remote. Each of them corresponds to a work area:

  1. Working Directory: It is the area corresponding to the modified state and it is the local folder of your computer where you store the files of your project.
  2. Staging Area: It is the area corresponding to the staged state, it is also called index because it is the area where git indexes and adds the changes made to the files prior to committing them in its registry.
  3. Local Repository: It is the area corresponding to the committed state, where the changes have already been registered in the git repository. It is also called HEAD because it indicates in which change the repository pointer is located.
  4. Remote Repository: It is the area corresponding to the remote state and it is the remote directory where we store the project files on a web platform such as GitHub, GitLab, BitBucket. Git namesthe remote repository origin.
GIT Flow
# add the changes of a file to the staged
git add directory/file
# add all changes from all files to staged
git add .

# changes are committed to the repository
# you must write the change message
# when the configuration file is opened
# when finished save and close the file
# for the changes to take effect
git commit
# is a shortcut of the previous command
# write and commit the change message in one step
git commit -m "message describing the change"

# add the remote origin of your GitHub repository
git remote add origin https://github.com/user/repository.git
# the first time we link the remote repository to the local one
git push -u origin main
# for subsequent updates, if you don't switch branches
git push

# to download the changes from the remote repository to the local one
git pull
Help
# help in terminal
git -h
# help in the browser (X is a command)
git help X
Ignore Files

In the file .gitignorewe include everything that we do NOT want to include in our repository. We can create it manually or with gitignore.io.

# this is a comment
file.ext
folder
/file_from_root.ext

# ignore all files ending in .log
*.log
# except production.log
!production.log

# ignore files ending in .txt inside the doc folder,
# but not in its subfolders
doc/*.txt
# ignore all files ending in .txt inside the doc folder
# and also in its subfolders
doc/**/*.txt
Clone Repositories
git clone https://github.com/user/repository.git
Branches

A branch allows us to isolate new functionality in our code that we can then add to the main version.

# create branch
git branch branch-name

# change branch
git checkout branch-name

# create a branch and switch to it
git checkout -b branch

# delete branch
git branch -d branch-name

# remove remote branches
git push origin --delete branch-name

#delete branch (forced)
git branch -D branch-name

# list all branches of the repository
git branch

# list branches not merged into the current branch
git branch --no-merged

# list branches merged into the current branch
git branch --merged

# rebasing branches
git checkout child-branch
git rebase main-branch
Merges

Join two branches. To make a merge we need:

  1. Go to the branch that will keep the merged content.
  2. Fuse.

When branches are merged, 2 different results can be given:

  • Fast-Forward: The merger is done automatically, there are no conflicts to resolve.
  • Manual Merge: The merge must be done manually, to resolve content duplication conflicts.
# switch to the main branch that will remain from the merge
git checkout main-branch

# run the merge command with the secondary branch to merge
git merge child-branch
Changes

You can add modifications to the last change

# without editing the message of the last commit
git commit --amend --no-edit

# editing the message of the last commit
git commit --amend -m "new message for last commit"

# remove the last commit
git reset --hard HEAD~1

We can scroll the repository history backwards or forwards in changes or branches, without affecting the repository itself.

# switch to a branch
git checkout branch-name

# switch to a particular commit
git checkout id-commit
History Record

git logIt allows us to know the entire history of a project, with information on the date, author and id of each change.

# change the n to any integer and it will show the n recent changes
git log -n

# display changes made after the specified date
git log --after="2019-07-07 00:00:00"

# display changes made before the specified date
git log --before="2019-07-08 00:00:00"

# display changes made in the specified date range
git log --after="2019-07-07 00:00:00" --before="2019-07-08 00:00:00"

# display a graph of the change history, branch and merges
git log --oneline --graph --all

# show all log actions
# including inserts, changes, deletes, merges, etc.
git reflog

# differences between the Working Directory and the Staging Area
git diff
History Reset

We can remove the project's change history forward relative to a baseline.

# shows us the list of new files (untracked), deleted or edited
git status

# delete HEAD
git reset --soft

# delete HEAD and staging
git reset --mixed

# delete everything: HEAD, Staging and Working Directory
git reset --hard

# undo all changes after the indicated commit, preserving the changes locally
git reset id-commit

# flush all history and return to the specified commit
git reset --hard id-commit
Reset Repository

If at any time you need to reset the change history of a repository so that it looks as if you had just created it, execute this series of commands:

cd folder-repository
mv .git/config ~/saved_git_config
rm -rf .git
git init
git branch -M main
git add .
git commit -m "Initial commit"
mv ~/saved_git_config .git/config
git push --force origin main
Remote
# show the remote sources of the repository
git remote

# show remote sources in detail
git remote -v

# add a remote source
git remote add source-name https://github.com/user/repository.git

# rename a remote source
git remote rename old-name new-name

# remove a remote source
git remote remove source-name

# download a remote branch to a different local branch than the main one
git checkout --track -b remote-branch origin/remote-branch
Tags

With this option git allows us to version our code, library or project.

# list tags
git tag

# create a label
git tag version-number

# remove a tag
git tag -d version-number

# display information about a label
git show version-number

# synchronizing the tag from the local to the remote repository
git add .
gittag v1.0.0
git commit -m "v1.0.0"
git push origin version-number

# generating an annotated tag (with commit message)
git add .
git tag -a "v1.0.0" -m "Tag message"
git push --tags
GitHub Pages

gh-pages is a special branch for creating a website to your project hosted directly on your GitHub repository.

Para crear esta rama especial en GitHub ejecutamos los siguientes comandos:

git branch gh-pages
git checkout gh-pages

git remote add origin https://github.com/user/repository.git
git push origin gh-pages

# to download the changes from the remote repository to the local one
git pull origin gh-pages
Collaboration On GitHub

In order to collaborate on projects hosted on GitHub, we need to make use of forks and pull requests, tools that the platform offers us for this purpose.

Below I describe the collaboration process on GitHub.

  1. Fork the repository you want to contribute to, to do so, follow the instructions at this link.
  2. Once the repository is forked in your GitHub account , clone it to your computer.
  3. In the local repository you have to configure the remote origins of your new copy to have both remotes, the originals (origin) and those of your copy, to do so, follow the instructions in this link.
  4. Create a new branch in your local fork to do your collaboration, and sync it with your remote repository, to do so, follow the instructions at this link.
  5. Configure your repository to accept changes (pull requests), to do so, follow the instructions in this link.
  6. Create a pull request, to do so, follow the instructions at this link.
  7. Wait for the owner of the original repository to accept your changes.
  8. Once your pull request is accepted, it is recommended that you delete the branch you worked on the change on and update your forked repository with the changes from the original repository.

I have attached a summary of the commands to execute to collaborate in a GitHub repository:

# fork repository
git clone https://github.com/user/repository.git
git remote -v
git remote rename origin fork
git remote add origin https://github.com/user/repository.git
git checkout -b new-branch
git push fork new-branch
# request the pull request
# accept the pull request
git checkout main
git pull origin main
git push fork main
git branch -d new-branch
git push fork --delete new-branch
More

Here are some links where you can deepen your knowledge about Git and GitHub: