Learning-Git

Table of contents

Setting up your git bash

The first steps is to find the git bash application that you installed previously

After running git bash you will have to configure it with your GitHub user name and email address like this.

git config --global user.name "your username"
git config --global user.email "your email"

To confirm that your data was successfully saved you can execute one of the following commands.

NOTE: some give more information than others.

git config --global --list
git config --list

Initialize a repository locally

  git init

View the status of modified files

  git status

To add a file to the stage we use the following command

  git add <name-file>

To add to the stage all the files that have been edited, use the following command

  git add .

Perform a commit (code capture) with a message about the changes that were made

  git commit -m "Message commit"

If we want to skip the two previous commands ( git add, git commit -m ) we can do the following, it is like watching a shorthand

  git commit -am "Message commit"

It is possible to remove them in several ways, either by deleting the file from the folder, but it may not always be so because maybe that file only added a couple of new lines or that file had to be added in a separate commit, then git makes it easy to remove that file from the staging area with this command:

  git restore --staged <name-file>

Now we will have to create a git repository to be able to add our project

  1. Click on the “new repository” option

add-1

  1. A name is required for the repository, so we insert one, GitHub is responsible for seeing if that name we have already used before, if not if it validates the repository name field

NOTE: The repository view option must be public, otherwise no one will be able to see our project

add 2

  1. Click create repository

add 3

A space like this will appear:

code init

The following command is to create a README file to our project (if necessary) otherwise we can skip this step:

  echo "# test-repository" >> README.md

To create the main branch in our project you need mandatory use of the following command because the ” -M “ option is to force it to move to the main branch

  git branch -M main

Now we add the path in which our staging area will connect to our remote repository through this command

FOR EXAMPLE: git remote add origin https://github.com/Noriega402/test-repository.git
  git remote add origin url

Finally we make all changes or new files are uploaded to our repository on GitHub:

  git push -u origin main

Once these steps have been done we can go to our repository on GitHub and refresh the page.

Other commands

git push

standard

The most commonly used to upload the first commit

git push -u origin <branch-name>
--force

To forcibly upload changes

git push -f

NOTE: Do not use the -f flag unless you are absolutely sure of what you are doing. It can cause a lot of problems.

--all

To upload all the changes you made in all the branches.

git push --all
--tags

To upload all the tags you have created.

git push --tags

git commit

-m

To be able to create a commit message without using a code editor

git commit -m "Your message commit"
-am

In addition to including the commit message, this option allows you to skip the staging phase. The addition of -a will automatically stage any files that are already being tracked by Git (changes to files that you’ve committed before).

git commit -am "Your message commit"
--amend

Most of the time it happens to us for the commit message we write it wrong, then this command helps us to correct the commit again.

  git commit --amend -m "Fixed commit message"
--no-edit

In case we forget to add a file or folder to a commit, we can also add it this way with a new option and there is no need to edit the commit message or add a new one:

  git add name-file
  git commit --amend --no-edit

git clone

--branch

Use it to be able to clone a specific branch of a repository.

git clone --branch <nameBranch> <urlResporitory>

Example:

git clone --branch docker https://github.com/Noriega402/Node-API-REST.git

or the short form:

git clon -b <nameBranch> <urlResporitory>

git diff

diff

It is used to be able to see in console the changes made, either we delete or add new lines of code

  git diff
diff "file"

We can use it to see the changes made in a single file

  git diff <name-file>
diff hash-old hash-new

We can use it to see the changes made between one commit and another

  git diff <hash-commit-old> <hash-commit-new>

git rm

You have several options which are as follows:

-r
--cached
-rf

git --log

We have many changes or commit in our repository, as the project gets bigger, my project will have more commits, the good thing about this is that each commit has a message to know what changes were made, but how to see the name of those messages and see the hash of each one, we have several ways:

--oneline
--stat

--patch

--graph

joins log

git branch

branch

View local branches

View all branches

-v

New branch

New name branch

checkout

Rename branches

--move

Form One

  git branch --move old-name-branch  new-name-branch

Form Two

  git branch -m old-name-branch  new-name-branch

Deleting branches

-d
  git checkout main
  git branch -d development
  git branch

git stash

Saves the current Staging job in a list designed to be temporary called Stash, so that it can be retrieved in the future. To add the changes to the stash, use the command:

git stash

We can put a message in the stash, in order to differentiate them in the git stash list in case we have several elements in the stash. This with:

git stash save "Your messagge to stash"

list of items in the stash

To see the list of changes saved in Stash and thus be able to retrieve them or do something with them we can use the command:

  git stash list

Get elements from the stash

The stashed behaves like a data Stack behaving in a LIFO (Last In, First Out) way, so we can access the pop method.

The pop method will retrieve the last stashed state from the list and insert it into the staging area, so it is important to know which branch you are in to be able to retrieve it, since the stash will be agnostic to the branch or state you are in. It will always retrieve the changes you made in the place you call it.

Creating a branch with the stash

To create a branch and apply the most recent stash we can use the command:

git stash branch <branch_name>.

If you want to create a branch and apply a specific stash (obtained from git stash list) you can use the command:

git stash branch branch_name stash@{<num_stash>}

By using these commands you will create a branch with the name , move to it and have the specified stash in your staging area.

Removing element from the stash

To remove the most recent changes within the stash (element 0), we can use the command:

git stash drop

But if, instead, you know the index of the stash you want to delete (via git stash list) you can use the command:

git stash drop stash@{<num_stash>}

Where the is the index of the saved change.

git clean

If, on the other hand, you want to remove all elements from the stash, you can use:

git stash clear

Executing the default command may result in an error. Git’s global configuration forces the force option to be used with the command for it to be effective. This is an important safety mechanism as this command cannot be undone.

NOTE: git clean only detects new files, not just repeated files.

Simulate a file deletion

git clean --dry-run

Delete the files listed as not to be tracked.

git clean -f

Delete the folders listed as not to be tracked

git clean -df

git reflog

Git saves all changes even if you decide to delete them, by deleting a change what you are doing is just updating the branch tip, to manage these tips there is a mechanism called reference logs or reflogs…The management of these changes is through the reference (or ref) hash’s which are pointers to the commits…The hash’s record when the Git references were updated in the local repository (local only), so if you want to see how you have modified the history you can use the command:

git reflog

git reset

NOTE: with soft, deleted files are not recovered, they just remain deleted in staging.

  git reset --soft <hash-commit>

Note: with hard if the files are recovered inside our computer, as if they had never been deleted.

  git reset --hard <hash-commit>