Git and GitHub

Initial Considerations

Git is a version control system that allows you to track changes in your files and folders. It is a tool that allows you to save the changes you make to your files and folders, and to return to a previous version if necessary.

To configure git in order to set a user name and a email associate to the changes will be commited use git config --global user.name "{write your name here}"
Use git config --global user.email "{write the email address of your github account}"


Local repositories

Initializing Local Repositories

When starting a project use git init to initialize a local repository. You have to be in the folder where you want to create the local repository.

If you want to check what just happened after git init you can use ls -al to see all files (including the hidden ones). This command will list the files in your current directory. If you have already initialized a local repository with git init in your current directory by running the ls -al command you should see a .git/ directory in the list.

When running git init the staging area is created in your computer's RAM. There, all changes will be prepared for submission to the local repository, in this area the files start to being tracked. The local repository where the project files will be stored is also created.

working in a local repository

When working with local repositories there are three phases in the development environment:

Remote repositories

To create a remote repository you have to go to GitHub and create a new repository. You can create a new repository by clicking on the + icon in the top right corner of the page and then clicking on New repository. You can also create a new repository by clicking on the Repositories tab in the top menu and then clicking on the New button.

Once created the remote repository you can clone the files into your computer system by runninggit clone url to clone a repository in your local machine.


You can check the git phase by using git status

If you added some path by mistake, you can remove it using git rm --cached If it does not work, use git rm -f --cached {file_name.extension}

To send the files or updated changes use git commit -m "{message}"

To see the history of an archive use git log file_name.extension

To see the changes in a file use git show fileName.extension

to see differences between versions use git diff IDcommit1 IDcommit2

To start writing press Esc + i To go out of vim press at once Esc + Shift + z + z

git log --stat to see specific changes.

Q

Restoring changes

checkout commitID is used to created branches or go back to a previous version conserving commits.the commit ID makes reference to the ID commit where you want to go back.

git reset not only goes back in the changes but also deletes all the subsequent commits. it will deleted all te changes done after commitID (including commits)
git reset commitID. git reset commitID --soft : deletes changes until staging area.
git reset commitID --mixed : deleted changes until working area (untracked area).
git reset commitID --hard :go back until commitID information.
git reset head: takes out the files of staging area. We can use git add . to sent back the files to the staging area.

git rm --cached: deletes files from our staging area and conserves them in the hard disc.
git rm --force: deletes completely the file. It is possible go towards the existing files register.

master or main is your principal branch.

merge merging changes between branches.

when merging changes between branches it is possible the raise of conflicts.

rm -rf .git

Branches

To create a branch use git branch branch_name

To change between branches use git checkout branch_name

When you add changes in a branch is and you want to change of branch you have to adding and commit that changes before change of branch-if not you could miss your changes

It is a good practice to update your local repository before sending changes to remote repository (before git push) so you should first run git pull origin {branchName}. Once your local repository be updated you must solve conflicts if exist and after send changes git push

Public and Private Keys--asymmetric one-way encryption

powerful password cracking. ssh protocole

Git show

To see the commits made we use: git log

To see all the commits use: git log --all

git branch

When merging branches you must check that you are than the branch where you want to do the merge and use the command: git merge branch1 -m "message"

git remote add origin {url}> git remote git remote -v git pull origin branchName git push origin branchName git config -l

Generating SSH Keys

to generate ssh key visit: Adding a new SSH key to your GitHub account

After generating ssh keys set the url asociated to the remote repository

git remote set-url origin {url} git diff

use git log --all --graph to see the flow through the branches

to a better visualization use: git log --all --graph --decorated --online

to assign alias use: alias {assignedName}="{command}"

Labels

to created a label use

git tag -a {label} -m "{message}" {commitIDassociated} git show-ref --tags history git push origin --tags git tag

to see labels

git tag -d {labelName}

delete a label in our local repository

to delete a tag in github from git bush, after using git tag -d {labelName} use: git push origin :refs/tags/{labelName}

to edit files from the console use vim {file_name}

staging server

feature branch

tow branches in parallel

pull request

DEVops

fix typo

Entidades HTML: para tíldes. &{a}acute;

fork: bifurcaciones

stash

auto request to update a fork or copy the url and in the terminal running git remote add {Name(upstream)} {url}

git pull {upstream} {master}

.gitignore

rebase: only for local repositories.

git rebase master. It changes the hystory of the branch

rebase

It recommendable use it only in local repositories

git branch -D branchName to eliminate a branch

stash (a commit back)

git stash saves the changes in somewhere

git stash list

git stash pop

git stash branch branchName

git stash drop

git clean --dry-run simulation

git clean -f

git log --oneline

git cherry-pick commitID


git reflog All the changes

git reset --HARD commitID

git reset --sauf

git commit --amend

git commit --amend

git grep word

git grep -n word

git grep -c word

git log -S word

git grep -c word

git shortlogcommits per person

git shortlog -sncommits per person

git shortlog -sn --allcommits per person

git shortlog -sn --all --no-mergescommits per person

git config --global alias.stats "command fragment"

git blame fileName file history

git blame -c fileName file history

git blame fileName -startLine,endLine file history

git branch -r remote branches

git branch -a local and remote branches

git command --help