Git reference
What is Git?
Git is a version control system that is used for software development and other version control tasks
Read more at: https://git-scm.com/about#small-and-fast
Purpose of this referencerguide
- With a reference guide, you can quickly search for and find commands and functionality, that you need, but don’t know about.
- The original documentation / reference guide is ‘long’, if you only need to find a single command.
- The original documentation / reference guide don’t give testable code examples, which you can experiment with and learn from.
- The multiple interactive tutorials are great, when you run them for the 1st time, but are to slow, when you need to look something up again.
Why Git without a GUI client?
The benefit of knowing the command line:
- It’s the same on every OS.
- You always have access to the command line, no matter, which GUI the customer/coworkers use.
- You can be sure it works as intended.
Download and install git
Navigate to https://git-scm.com/downloads
This guide uses git bash (so please install it)
Command | Description | Example |
cd <folder name> | Enter a folder named <folder name> | mkdir my_folder cd my_folder cd .. rmdir my_folder |
cd .. | ”cd ..” makes you enter the parent folder | mkdir my_folder cd my_folder cd .. rmdir my_folder |
dir | ”dir” lists all content in current folder. | mkdir my_folder cd my_folder mkdir another_folder dir cd .. rmdir my_folder |
mkdir <folder name> | Make a new folder named <folder name> | mkdir my_folder rmdir my_folder |
rmdir <folder name> | Remove a folder named <folder name> | mkdir my_folder rmdir my_folder |
cd /d | Navigate to drive d
(Windows only) |
cd /d |
git link | Get your git link, from github, bitbucket or where you have your data. | – |
clone <git link> | 1. navigate to your workspace folder like c:\git-workspace
2. clone <git-link> < |
cd /c mkdir git-workspace cd git-workspace clone git https://…/git-test.git |
git status | Untracked: will not be pushed to servernew (green): will be pushed to servermodified (red): have been changed, but will not be pushed to server. modified (green): |
(Requires cloned folder)
git status |
git add <filename> | <filename> will be staged (marked ready) for the next commit.
Use * if you want to add all files. |
(Required a cloned folder)
git status |
git commit -m ”<write your message>” | Commits your stage, which will be included in the next push to server. | (Required a cloned folder)
echo version_1 > file.txt |
git config –global user.email “your@email.org” | Changes your email | git config –global user.email “your@email.org” |
git config –global user.name
“your_git_name” |
Changes your git name | git config –global user.name “your_git_name” |
git push origin <branch> | Push your comitted data to the server.
<branch> is normally ”master” |
(Required a cloned folder)
echo version_1 > file.txt |
git pull origin <branch> | Pull data from server and merge with your folder
<branch> is normally ”master” |
cd /c/git-workspace mkdir folder_a cd folder_a git clone git https://…/git-test.git cd .. mkdir folder_b cd folder_b git clone git https://…/git-test.git echo version1 > file.txt dir git add * git commit -m “version 1” git push origin master cd ..cd folder_a dir git pull origin master dir |
git reset –hard HEAD~1 | If you want to undo something that you have committed.
Warning: It will delete your current changes! Use git reset –soft HEAD~1 if you want to keep your changes. |
(Required a cloned folder)
echo version1 > file.txt |
git pull origin <branch> but this time with a conflict! |
It is possible to merge 2 conflicting files together with a pull |
cd /c/git-workspace mkdir folder_a cd folder_a git clone git https://…/git-test.git cd ..mkdir folder_b cd folder_b git clone git https://…/git-test.git echo version1 > file.txt git add * git commit -m “version 1” git push origin master cd..cd folder_a git pull origin master echo version2a > file.txt git add * git commit -m “version 2a” git push origin master cd ..cd folder_b echo version2b > file.txt git pull origin master vim file.txtYour file should look like this: “<<<<<<< HEAD Version 2b ======= Version 2a >>>>>>> ece37d2c042468b7b29147cc3af64189e5d12e41″ It shows the difference between the 2 files. Write the file, like you want, eg. “Version 2a & 2b” type :wq in Vim to save and exit. git add * git commit -m “version 2a & 2b” git push origin master |
git checkout <branch> | Change branch, if you want to make some experiments or changes, without disrupting the code of other developers. | (Required a cloned folder)
git status |