git console basics - IliaKobalia/QA-program GitHub Wiki
Git Console Basics
Basic Git Operations Step By Step
Precondition
Please make sure you completed Git installation and configuration from the Environment Setup section. If you want to learn more about Git configuration, read Git Config guide.
Initialize a Project Git Repository
Link: Git init
Useful to know: Git clone
Create and open a new project directory:
mkdir git-test
cd git-test
Create a new Git repository inside the project directory:
git init
Make a Commit to Git Repository
Link: Saving changes in Git & Git commit
Useful to know: .gitignore
Create an empty README.md
file:
touch README.md
See that the file shows in the untracked (unstaged) file list:
git status
Add the file to the list of staged files ready for commit:
git add .
Check how the file status is different now:
git status
Commit all staged files to the current active branch:
git commit -m ‘Initial commit’
Check that your commit is in the git log:
git log
Create a Local Branch
Link: Git Branch
Create a new branch called login-suite
:
git branch login-suite
List all branches in the repository:
git branch
Switch from the master
branch to the login-suite
branch:
git switch login-suite
Check that login-suite
is an active branch in the branch list:
git branch
Merge Local Branches
Link: Basic Branching and Merging & Git merge
Create, stage, and commit a new file to the login-suite
branch:
touch login.spec.ts
git add .
git commit -m 'Added the login test suite'
Switch back to the master
branch:
git switch master
Merge the login-suite
branch with the master
branch:
git merge login-suite
Remove the login-suite
branch
git branch -d login-suite
Check remaining branches in the repository:
git branch
Sync to a Remote Repository
Link: Git remote
Useful to know: Git push & Git pull
Requires: a remote repository on GitHub, GitLab, or other repository service
Requires: Upload your SSH Key step to be complete for the repository service
Link your local repository to a remote repository:
git remote add origin <your-remote-repo-url>
List the remote connections you have to other repositories:
git remote
Push all your commits from the local master branch:
git push -u origin master
Option
-u
sets up local branch master to track remote branch master. Now you can run argument-lessgit push
andgit pull
for this branch.
You will be asked for your login and password if you skip the Upload your SSH Key step
To pull changes from the remote repository master branch, run:
git pull
List all branches including remote:
git branch -a
Work with Remote Branches
Link: Using Branches
Useful to know: Git fetch
Create the local branch profile-suite
:
git branch profile-suite
Switch to the newly created branch:
git switch profile-suite
Make some changes to the profile-suite
branch:
touch profile.spec.ts
git add .
git commit -m 'Added the profile test suite'
Push the 'profile-suite' branch and its changes to the remote repository:
git push -u origin profile-suite
Switch to the master
branch, marge profile-suite
with master
, and push changes to remote repository:
git switch master
git merge profile-suite
git push
Remove local branch profile-suite
git branch -d profile-suite
Note that the remote profile-suite
branch is still available:
git branch -a
And you can still download this branch from the remote:
git switch profile-suite
Remove remote branch origin/profile-suite
:
git push -d origin profile-suite
Switch back to master
branch:
git switch master
And remove local branch profile-suite
:
git branch -d profile-suite
What to do next?
We encourage you to continue playing with the git console commands until you feel comfortable with the basic operations. Understanding how Git works, and knowing the commands, saves a lot of time when a GUI produces unexpected results or cannot do what you are looking for.