GIT
Cheat Sheet — Git Commands Used in This Section
In this section, you created and worked with a local repository.
You used the following Git commands
Initialize an empty Git repository:
git init
Show the status of your repository:
git status
Stage a specific file:
git add readme.txt
Stage all changed files:
git add .
Commit the staged files:
git commit -m "Create readme file"
Define notepad as an editor. It will be used when you run the git commit
command without -m
parameter:
git config --global core.editor notepad
Show the changes of a specific file:
git diff readme.txt
Show the changes in your working directory:
git diff
Show the changes in your staging area:
git diff --staged
Show the history/log:
git log
Show the history/log with one commit per line:
git log --pretty=oneline
Checkout a specific commit by its snapshot hash:
git checkout b346471
Navigate back to your main branch:
git checkout main
Now you know how to work with a local repository.
Create a .gitignore file
#ignore all .txt files- *.txt
# But don’t ignore the readme.txt — !readme.txt
# Ignore only .txt files in the root folder- /*.txt
# Ignore all .txt files in folders with the name generated- generated/*.txt
#Include also sub directories: generated/**/*.txt
# Ignore all files in folders with the name generated- generated
Summary
Create a local repository
- Stored in hidden .git folder
2. Understand the three main areas
- Repository
- Working Directory
- Staging area
3. Work with your repository
- create your first commit
- look at the history of your code
- Ignore files in the working directory
QnA
- How do you show the history of your repository with every commit printed nicely on a single line?
A. git log — pretty=oneline
2.You looked at the history of your code and you found a commit with this hash value: 8a64a5d6be4862c035baa67b5a99761d151c0d82. How can you load the code of this commit into your working directory?
A. git checkout 8a64a5
3 .After looking at a specific commit, you want to go back to your main branch. Which command do you execute?
A. git checkout main
4. You don’t want to add the compiled code of your project to the repository. How can you ignore that code when you call “git add .”?
A. With a .gitignore file
Branching and Merging Code
In the next section, you will learn how to branch and merge your code. This can be helpful when you want to work on different features in parallel. But before that, let’s test your knowledge with some learning questions.
- Create a Branch- implement a feature ub this branch
2. Merge the branch into the main branch
3. Handle merge conflicts
In this section, lets see how to branch and merge the code.
You used the following Git commands
List the local branches:
git branch
Create a new branch with the name “feature/AddTwitterHandle”:
git branch feature/AddTwitterHandle
Checkout the new branch:
git checkout feature/AddTwitterHandle
Checkout the main branch:
git checkout main
Merge changes from the feature branch into the main branch. This command assumes that you have the main branch checked out:
git merge feature/AddTwitterHandle
Show a graph when showing the history:
git log --pretty=oneline --graph
Now you know how to branch and merge your code, which can be helpful when you want to work on different features in parallel.
In the next section, you will learn how to push your code to a remote repository on GitHub. But before that, let’s test your knowledge with some learning questions.
Check your knowledge of branching and merging code with the following learning questions.
- Which command do you use to show your local branches?
A. git branch
2.How do you create a branch with the name “MyNewFeature”?
A. git branch MyNewFeature
3.You have created a new branch with the name MyNewFeature. How do you switch to that branch?
A. git checkout MyNewFeature
4. You implemented a feature in the branch MyNewFeature. After that you checked out the main branch. Now you want to merge your MyNewFeature branch into the main branch. Which command do you execute?
A. git merge MyNewFeature
Pushing to Remote Repository
1.Create a repository on Github
2. Work with github repository
- push code to the repository
- Clone the repository
- Pull changes from the repository
- git pull = git fetch + git merge
3. Understand pull command
-Fetch and merge
How to push your code to a remote repository on GitHub.
Use the following Git commands
Add the remote repository https://repositoryURL under the name origin to your local repository:
git remote add origin https://repositoryURL
Push the first time to the main branch of the remote repository and set it as upstream:
git push -u origin main
Push the next commits to the remote repository:
git push
Clone a remote repository with the URL https://repositoryURL into a local repository on your machine:
git clone https://repositoryURL
Clone a remote repository into the current directory (Note the separate dot at the end that indicates the current directory):
git clone https://repositoryURL .
Fetch changes from the remote repository without merging them into your local branch:
git fetch
QnA
Question 1:
You want to copy the GitHub repository https://repositoryURL locally to your machine. Which command do you use?
A. git clone https://repositoryURL
Question 2:
Let’s assume you initialized a local repository and you created a few commits. Now you want to add the remote repository https://repositoryURL under the name origin, so that you can push to it. How do you add the remote repository?
git remote add origin https://repositoryURL
Question 3:
With the command “git remote”, you can show the names of your remotes. How can you also see the fetch and push URLs?
A. git remote -v
Question 4:
Let’s assume that you added a remote repository to your local repository with the name origin. Now you want to push the first time to that origin repository into a main branch. You also want to set that repository and the main branch as upstream for your local branch. Which command do you use?
A. git push — u origin main
Question 5:
The “git pull” command executes which two commands behind the scenes?
A. git pull = git fetch + git merge
Question 6:
Let’s say that you want to download the latest commits from the remote repository, but you don’t want to merge them yet into your local branch. Which command do you use?
A. git fetch
Now we know how to push code to a remote repository on GitHub, how to clone a remote repository, and how to pull changes from the remote repository into your local repository.
In the next section, you will learn how to create so-called pull requests.
Creating and Merging Pull Requests
- What is a pull request?
- Please merge my branch
2. Create and merge a pull request.
3. Fork a repository on github
QnA
Question 1:
A pull request is a request to do what?
A. To pull (=fetch+merge) a feature branch into the main branch.
Question 2:
What is a fork on GitHub?
A. a copy of repository
Question 3:
When you create a public repository on GitHub, what are the rights?
A. anyone can see it, but only you can write to it.