Git Basics

Contents
Common user commands
config: Set Git username
--local user.name: Run as different user
init: Initialize Git repository
status: Current repository state
add: Add file to staging area
reset: Remove file from staging area
restore: Remove uncommitted changes
commit: Commit changes
checkout -- . : Restore last commit
chechout .: Recover a deleted file
diff: Examine changes
.gitignore file: Ignore certain files
git hash-object
Other Git articles

Popular commands

Check current status

git status

Add file to staging area

git add index.html

Remove file from staging area

git reset file.txt

Commit with comments

git commit -m 'Updates www.aredel.com/git/ contents'

Add to staging area and commit with a sinlge command

git commit -am 'Updates www.heihei.ru/en/Spain/'

Push to main

git push origin main

Add key to agent

ssh-keygen eval `ssh-agent -s` ssh-add id_rsa

User

Set the active user name

$ git config --global user.name "Andrei"

Check the active user name

$ git config user.name

Andrei

Set active user email

$ git config --global user.email "www.aolegovich.ru@aredel.com"

Local user

If you need to push from another user, you can use user.local for a specific repository

First, you need to create a key pair and add the public key to the repository.

LetEs give the key a name keyname and act according to this instruction

ssh-keygen

Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Andrei/.ssh/id_rsa): keyname Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in keyname Your public key has been saved in keyname.pub The key fingerprint is: SHA256:cvv+vna9oR1T9tueN9pPKzS6VzINYAiYGKcUZ2uo7Ok Andrei@urnsulab The key's randomart image is: +---[RSA 3072]----+ | ==oo.. . | | ..=o. . o | | o o . . | |. . . . | | o . S . o| |. . o . = +o| | o . o ==o| |. . ..o=o@| | E .o=*=oOO| +----[SHA256]-----+

eval `ssh-agent -s`

Agent pid 2261

ssh-add ~/.ssh/keyname

Identity added: /c/Users/Andrei/.ssh/keyname (Andrei@urnsulab)

git clone git@github.com:AndreiOlegovich/robot_web_ui_test.git

Cloning into 'robot_web_ui_test'… warning: You appear to have cloned an empty repository.

cd robot_web_ui_test/ git config --local user.name "AndreiOlegovich" git config --local user.email "info@eth1.ru" git config --local core.sshcommand 'ssh -i ~/.ssh/keyname -F /dev/null' git status

On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)

echo "# robot_web_ui_test" >> README.md
git add README.md

warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it

git commit -m "first commit"

[main (root-commit) 05fe1fe] first commit 1 file changed, 1 insertion(+) create mode 100644 README.md

git push -u origin main

Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 245 bytes | 245.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:AndreiOlegovich/robot_web_ui_test.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.

init

Start Tracking Changes - Initializing or Getting Started with Git

$ git init

Initialized empty Git repository in C:/Users/aolegovich/Desktop/Sites/hello-world/.git/

By default, the repository is stored in a subdirectory named «.git» in the root directory of the working copy of the file tree stored in the repository.

Any file tree in the system can be turned into a git repository by issuing a command to create a repository from the root directory of this tree (or by specifying the root directory in the program parameters)

It is important to understand that the repository must be initialized in the directory with the project.

There can be dozens of projects on one computer and each of them can have its own repository, which, in turn, can be connected to github, gilab or somewhere else.

By moving from one directory to another, you move between these repositories. Thanks to the files .gitinit git automatically knows that you are already in a different place and working on a different project.

You can configure your terminal bash or zsh to show you which repository you are working on and which branch is active.

Create file

$ touch index.html

status

View the latest changes in the repository

$ git status

If you are only interested in the current directory

git status .

On branch master

No commits yet

Untracked files:
   (use "git add <file>..." to include in what will be committed)

        index.html

nothing added to commit but untracked files present (use "git add" to track)

The file in which changes occurred is highlighted in red.

add

Add a file to the group of files you plan to commit (staging area)

$ git add index.html

If there are many files and you want to add all use

git add -A

Add all files from the current directory

git add .

View the latest changes in the repository

git status

On branch master

No commits yet

Changes to be committed:
   (use "git rm --cached <file>..." to unstage)

        new file: index.html

The file that was added to the staging area is highlighted in green

Remove file from staging area

If you added something extra

$ git reset filename

restore: remove unwritten changes

If a file is added to the staging area, and all unwritten changes need to be discarded, the restore command will do.

$ git restore имя_файла

Example

touch test.txt git add test.txt echo "abc" >> test.txt cat test.txt

abc

git restore test.txt
cat test.txt

The file returned to its original state - it became empty.

commit

Commit changes - write changes to a file to the repository.

$ git commit -m 'Мой первый коммит.'

[master (root-commit) e2d3195] My first commit.
1 file changed, 1 insertion(+)
create mode 100644 index.html

Revert a file to its last committed state

$ git checkout -- .

You can restore a deleted file if it is not committed by running

$ git checkout .

clone

Clone remote repository

$ git clone https://github.com/Project/Project.git

Cloning into 'welcome-to-git'...
remote: Enumerating objects: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 9
Unpacking objects: 100% (9/9), done.

Or via SSH

$ git clone git@gitlab.com:Project/Project.git

The syntax is selected depending on the authorization type: password, key, etc.

diff

If you have made changes, you can examine them with the command

git diff

diff --git a/andrei/README.md b/andrei/README.md index de2ffa9..0741f41 100644 --- a/andrei/README.md +++ b/andrei/README.md @@ -69,4 +69,6 @@ Result Database Image Database +### Git Tutorial +[git-scm](https://git-scm.com/docs/gittutorial)

If you add these changes to the staging area, you can no longer see them with the same command.

git add -A
git diff

Need to add a flag --cached

git diff --cached

diff --git a/andrei/README.md b/andrei/README.md index de2ffa9..0741f41 100644 --- a/andrei/README.md +++ b/andrei/README.md @@ -69,4 +69,6 @@ Result Database Image Database +### Git Tutorial +[git-scm](https://git-scm.com/docs/gittutorial)

Banner Image

.gitignore

It is often undesirable to track every single change. Logs, temporary files, environment files or secret files can be better left private.

If you want to focus on the specific files, you can create a file .gitignore and add there the extensions of the files that you will not add to the repository.

An example of a file .gitignore in which we tell git not to track errors, logs, the .tmp directory and modules nodejs

# Ignoring:
*.err
*.log
node_modules/
.tmp/

For more information on creating rules for .gitignore read the article

«Syntax .gitignore»

git hash-object

$ echo "HeiHei.ru" | git hash-object --stdin

0bdb2c54a9617d62b661102a4862b417d7bdde9b

Related Articles
Git
Установка в Windows
Установка TortoiseGit
.gitignore
Необходимые Bash команды
Errors
Development
DevOps
SSH
Работа с API GitHub

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedback and Questions in Telegram

@aofeedchat