How to work with Git
Welcome to the Git lecture. Here you'll find a short script to the lecture with Git commands and useful links. Let's go!
Setting up the repository
Difficulty: Beginner Objectives: How to create and clone the repoBefore 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.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
Use
git remote show origin
to see remote repository URL.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.
Common git
commands
Difficulty: Beginner Objectives: How to create commits and branchesTo add changed files to the staging area we use
git add
command + file name. You may usegit add .
command to add all changed and untracked files.To create a commit use
git commit
command. To add a description message usegit commit -m "Your message here"
Use
git branch
command to see local branches andgit checkout branch-name
to switch to the branch.
To create a new branch and switch to it usegit checkout -b branch-name
command.git log
command shows commit history of your branch.To update your remote branch use
git push
command. If your local branch doesn't exists on the remote you might need to rungit push --set-upstream branch-name
or justgit push -u branch-name
, which means the same. Otherwise you may explicitly set a remote branch you want to push your changes to, likegit push origin branch-name
.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, andgit stash pop
with apply your changes back.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 usinggit merge branch-name
command). You may usegit pull
command to fetch changes froma remote branch and merge them at one time.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
.
Git flow
Difficulty: Intermediate Objectives: How to create pull requestsYou might need/want/like to follow the next steps during yout work with a repository:
Create branches - create a separate branch for every feature
Add commits - make small commits with a meaningful messages
Open a Pull request - open PR to discuss, review and approve your changes
Merge to the main branch
Undo the changes
Difficulty: Hard Objectives: How to revert or reset commitsTo undo you changes you may use two commands: git revert
or git reset
.
git revert HEAD
creates a new commit with the inverse of the last commit.git reset <commit>
clears the commit history until the given commit.