Daily use commands in git.
INITIAL SETTING
$ git config --global user.name "your_name lastname" $ git config --global user.email "youremail@something.com"
WORK WITH A REPO.
Start a repository
$ mkdir your_project $ cd your_project $ git init
Repository status
$ git status
Add a file (to a Staging area) to commit later
$ git add file
Remove the added file from the staging area & working directory
$ git rm --cached file
Remove the added file from the staging area, working directory & disk
$ git rm --force file
COMMITS
Make a commit
$ git commit -m "message about the commit"
Add modified files(add .) and a commit at the same time
$ git commit -am "test"
Check the last commit history and differences
$ git show
Check the differences between commits
git diff commitCode1 commitCode2
$ git diff b23b98a712 7a78e76d96f
LOGS
Check the log or commit history
$ git log file
commit 7a78e76d96f7575bf34839488a2c44c1c82f365a
Author: xilen0x <youremail@something.com>
Date: Fri Mar 4 17:50:59 2022 +0100
mod first commit
commit b23b98a7128c5e868abd78485b661b5ab803bcaa
Author: xilen0x <youremail@something.com>
Date: Fri Mar 4 17:31:12 2022 +0100
first commit
See the logs with other format
$ git log --stat
commit d26acafac7f96dfda97eb0bc0bb74d3a43d32e1d (HEAD, master) Author: xilen0x <youremail@something.com> Date: Sun Mar 6 17:31:28 2022 +0100 archivo v1 file | 2 — file.html | 15 +++++++++++++++ file2 | 4 —- style.css | 6 ++++++ 4 files changed, 21 insertions(+), 6 deletions(-)
To see the logs with a graphics design:
$ git log --all --graph --decorate --oneline
RECOVER
Go back in time with reset(be careful)
$ git reset b23b98a7128 --hard # Go back to commit b23b98a7128 (and delete the staging area as well). $ git reset b23b98a7128 --soft # o back to commit b23b98a7128 (and keep whatever is in the staging area).
Go back to just see a previous version
$ git checkout b23b98a7128
To come back again to the last version
# ex. to d26acafac7f96d $ git log master $ git checkout d26acafac7f96d
Delete files from the stage area (to not add them to the next commit).
$ git reset HEAD
BRANCHES
Create a branch
$ git branch branchName
Switch to a branch
$ git checkout branchName
List all branches / View the active branch
$ git branch
Merge master with a branch
$ git checkout master $ git merge branchName
Show branch history
$ git show-branch --all
Send a branch to github
$ git checkout branchName $ git push origin branchName
TAGS
Create a tag
$ git tag -a nombre-del-tag id-del-commit -m "version1"
Remove a tag
$ git tag -d nombre-del-tag
List tags created
$ git tag <- the tags names
or
$ git show-ref --tags <- the tags names and commits
Publish a tag in a remote repository
$ git push origin --tags
Eliminate a tag from the remote repository
$ git push origin :refs/tags/nombre-del-tag
ALIAS
Create a alias in a project
$ git config alias.logd "log --all --graph --decorate --oneline"
$ git logd
Create a alias globally
$ git config --global alias.logd "log --all --graph --decorate --oneline"
$ git logd
GITHUB
Step 1: Create a repository on github.
Step 2: Copy the URL of your new repo.
Step 3: Open the terminal and execute :
git add * git commit -m "your comment here" git remote add origin git@github.com:yourUser/yourRepository.git git push -u origin main