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 clonecommand + copied repository URL.In case you choose https it will look like:
git clone https://github.com/<my-username>/homepage.gitIn case of SSH:
git clone git@github.com:<my-username>/homepage.gitUse
git remote show originto see remote repository URL.git statuscommand 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 addcommand + file name. You may usegit add .command to add all changed and untracked files.To create a commit use
git commitcommand. To add a description message usegit commit -m "Your message here"Use
git branchcommand to see local branches andgit checkout branch-nameto switch to the branch.
To create a new branch and switch to it usegit checkout -b branch-namecommand.git logcommand shows commit history of your branch.To update your remote branch use
git pushcommand. If your local branch doesn't exists on the remote you might need to rungit push --set-upstream branch-nameor 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 stashcommand will add your changes to stash,git stash listwill show you all available stashes, andgit stash popwith apply your changes back.To follow you teammates changes you might need to run
git fetchcommand.git fetchdownloads 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-namecommand). You may usegit pullcommand 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 HEADcreates a new commit with the inverse of the last commit.git reset <commit>clears the commit history until the given commit.