How to work with Git

Translated into:
EN
UA

Viktoriia Vasylenko

Viktoriia works at Binary Studio as a Full stack developer after finishing Binary Studio Academy in 2020.

Hi, student! 👋
Welcome to the Git lecture. Here you'll find a short script to the lecture with Git commands and useful links. Let's go!
Level 1 back to top

Setting up the repository

Difficulty: Beginner Objectives: How to create and clone the repo
  1. Before you clone the repository set up either PAT (personal access token) or SSH keys. You may find this helpful -

    How to set up PAT,How to set up SSH keys.
  2. To clone the repositoty use git clone command + copied repository URL.

    In case you choose https it will look like:

    git clone https://github.com/<my-username>/homepage.git

    In case of SSH:

    git clone git@github.com:<my-username>/homepage.git
  3. Use git remote show origin to see remote repository URL.

  4. git status command will display the state of your repositoty and staging area. Use it to see which files are added to the staging area and are going to be included in commit.

Level 2 back to top

Common git commands

Difficulty: Beginner Objectives: How to create commits and branches
  1. To add changed files to the staging area we use git add command + file name. You may use git add . command to add all changed and untracked files.

  2. To create a commit use git commit command. To add a description message use

    git commit -m "Your message here"
  3. Use git branch command to see local branches and git checkout branch-name to switch to the branch.
    To create a new branch and switch to it use git checkout -b branch-name command.

  4. git log command shows commit history of your branch.

  5. To update your remote branch use git push command. If your local branch doesn't exists on the remote you might need to run git push --set-upstream branch-name or just git push -u branch-name, which means the same. Otherwise you may explicitly set a remote branch you want to push your changes to, like git push origin branch-name.

  6. Git stash is handy when you're not ready to commit yet. git stash command will add your changes to stash, git stash list will show you all available stashes, and git stash pop with apply your changes back.

  7. To follow you teammates changes you might need to run git fetch command. git fetch downloads commits, files, or new branches from a remote repository into your local repo. After that you may merge remote branch changes into your local branch (by using git merge branch-name command). You may use git pull command to fetch changes froma remote branch and merge them at one time.

  8. In case you forgot to add some files to your last commit or noticed a typo in a message you may use git commit --ammend.

Level 3 back to top

Git flow

Difficulty: Intermediate Objectives: How to create pull requests

You might need/want/like to follow the next steps during yout work with a repository:

  1. Create branches - create a separate branch for every feature

  2. Add commits - make small commits with a meaningful messages

  3. Open a Pull request - open PR to discuss, review and approve your changes

  4. Merge to the main branch

Level 4 back to top

Undo the changes

Difficulty: Hard Objectives: How to revert or reset commits

To undo you changes you may use two commands: git revert or git reset.

  1. git revert HEAD creates a new commit with the inverse of the last commit.

  2. git reset <commit> clears the commit history until the given commit.