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
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
NOTE: The repository view option must be public, otherwise no one will be able to see our project
A space like this will appear:
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.
The most commonly used to upload the first commit
git push -u origin <branch-name>
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.
To upload all the changes you made in all the branches.
git push --all
To upload all the tags you have created.
git push --tags
To be able to create a commit message without using a code editor
git commit -m "Your message commit"
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"
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"
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
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>
It is used to be able to see in console the changes made, either we delete or add new lines of code
git diff
We can use it to see the changes made in a single file
git diff <name-file>
We can use it to see the changes made between one commit and another
git diff <hash-commit-old> <hash-commit-new>
You have several options which are as follows:
git rm -r
git rm --cached name-file
git rm -rf name-folder
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:
git log
git log --oneline
git log --stat
git log --patch
git log -p
git log --graph
git log --graph
git log --oneline --graph
git log --oneline --graph --decorate
git log --oneline --stat
git log --oneline -p
git branch
git branch -a
git branch -v
git branch development
git checkout name_branch
There is a shortcut to create and move branches in the repository.
git checkout -b new-name-branch
Form One
git branch --move old-name-branch new-name-branch
Form Two
git branch -m old-name-branch new-name-branch
git checkout main
git branch -d development
git branch
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"
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
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.
git stash pop
git stash pop stash@{<num_stash>}
git stash apply stash@{<num_stash>}
NOTE: the
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
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
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.
git clean --dry-run
git clean -f
git clean -df
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
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>