Git Beginners: Workflow Basics and Basic Commands

The article “Git for beginners” is aimed at improving the skill of its use and correcting common errors.
Consider various situations in the life of the developer (and not only). Each will be accompanied by the correct optimal solution.
Git for beginners: basic commands

Git init
To create a subdirectory in the current directory .git
where all the repository files will be stored, run git init
. If the subdirectory already exists, a message will be displayed if you repeat the same command Reinitialized existing Git repository in
.
Git add
You need to add files with a specific extension to the project. You can add all the files one by one, but it is better to use *.<
extension_name>
to include all files with this extension:
git add *.py
If you want to add files with a specific extension and then specify a directory name, you can run the following command. It will add all .py files from the models/directory subdirectories :
git add models/\*.py
Git clean
Suppose you have created several new files or folders in the Git branch, and after a while, it turned out that these files are no longer needed. In this case, you need to clear your working tree of unnecessary files (which were added using git add ) with the following command:
git clean -df
To see which untracked files will be deleted, use the command:
git clean -dn
Git rm
The following command in the Git for Beginners collection will help remove tracked files:
git rm <file path>
If the file to be deleted is in the staging area, you need to apply a special flag:
git rm <file path> -f
In case you want to delete files from the repository, and leave them in your file system, run this command:
git rm --cached <file path>
Git branch
Have you made a typo in the name of the branch or do you want to change its name? The following command will help you:
git branch -m <old branch name> <new branch name>
If you need to change the name of the current branch:
git branch -m <new branch name>
To change the name of the start of a branch, you will need to perform several additional steps:
git push <name of the remote branch> --delete <old branch name> git push <name of the remote branch> <new branch name>
If the name of the local branch does not match the name of the branch in the repository, use the command:
git push <name of remote branch> <local name of branch>: <name of remote branch>
Git log
To see the Git commit history, use the git log command. You will be shown a lot of information, but among this, you only need a commit id and a message. To change the output, run the following command:
git log --oneline
It will show the output as in the picture below.

The first seven characters are the abbreviated commit id, followed by a message. Commit id is abbreviated because the “full version” consists of forty hexadecimal characters pointing to a 160-bit SHA1 hash.
Head -> master – means that we are in the master branch .
If you want to see the message of a particular author, you can do this:
git log --author="John Doe"
Git stash
Suppose you decide to check the branch code before making changes. For this there is a stash command that will keep the working tree in “clean”:
git stash
To undo the changes, do the following:
git stash pop
And if you change your mind, roll back:
git stash drop
Git checkout
To switch to another branch, run the command:
git checkout < branch name>
Do not forget to save changes or do a git commit in the current branch. If you do not, the changes may affect other branches, and you do not need it.
For example, there is a development branch, and you want to make a copy of it in order to switch to it directly. This can be done like this:
(development)$ git checkout -b < new branch name>
Git commit
You committed changes, later you realized that you made a mistake, or you just need to make the description more understandable. Then this command is what you need:
git commit --amend -m "message"
If you added new files or fixed an error, but do not want to add another commit message, use the command with the –no-edit flag :
git commit --amend --no-edit
You run your code into a remote repository, and then you realize that you need to change the commit message. To do this, after making changes, force push. Using the name of the remote repository as the source, run:
git push origin < branch name> -f
Git reset
If you need to cancel the last commit, use git reset . There are three useful reset flags:
- soft
- mixed
- hard

Suppose you want to discard the changes, before adding the file two . txt having commit id 96 b 037 c .
Now let’s run the git reset command with the – soft flag :
git reset --soft 96b037c
git reset – soft will “lose” all commits after this identifier (for example, 96b037c), but the files will not be deleted, they will be in the intermediate area.
Lost commits do not have a direct link to access them. Such commits can usually be found and restored with git reflog . Git will permanently remove the loss when the garbage collector starts (it runs every 30 days).
When you run git status, you will see:

If you run git log —oneline, you will see that the previous commits have been deleted:

If you execute the reset command with the –mixed flag , the commits will become “lost”, the files will not be in the intermediate area, but they can be found in the file system. If you do not specify a flag in the resetcommand at all , the –mixed flag will be used by default.
When you run git status see the following:

If you need to permanently delete files, run the reset command with the –hard flag .
It is recommended that you first perform a reset —soft to check the affected files. If all is well, run reset —hard .
Never use git reset <commit-id> after everything has been launched into the public repository. Deleting a commit that other team members continue to work with creates serious problems.
Git revert
Another way to cancel a git commit:
git revert <commit-id> --no-commit
Check affected files with git status. Then git commit -m “commit-message”.
Revert cancels not commit, but changes returned by the commit-id.

For example, you need to cancel the last commit. After revert, the status will look like this:

Before the last commit, the six .txt file was not added, so it was deleted, and five .txt returned to its previous state. Now the story will look like this:

If you want to cancel multiple commits within the range, run the following command:
git revert <old commit-id> .. <recent commit-id> --no-commit
If you need to cancel multiple commits, out of range, each commit-id should be specified :
git revert <commit-id-1> <commit-id-2> --no-commit
Git cherry-pick
And the last command from the Git category for beginners is cherry-pick . To make a commit (for example, correcting an error) in branch “A”, working in branch “B”, use the cherry-pick command.
First, you need to switch to the branch with a commit, copy commit-id, and then go back to your branch. Then run the following command to get the commit in the working branch:
git cherry-pick <commit-id>
Git fetch
The command for cases when it is necessary to collect all existing commits from the desired branch (remote repository) and save them in the local repository. After execution, git fetch
you get links to the remote project’s branches. The essence and the main advantage of the command is that with its help you do not merge all commits into the main branch.
Git merge
But git merge
just for this and need.
Git pull
This command is a symbiosis of git fetch
and git merge
. The shortcode is good, but only if you really need to merge commits right after they are collected.
Git push
If it is git pull
intended for merge changes to a local repository from a remote one, then it git push
works exactly the opposite: local changes are pushed to a remote repository.
You can also use:
git push origin master