2.22. tool - Git¶
Git is what’s called a Version Control System (VCS).
Git is not the same as GitHub or GitLab. Git is a tool in itself. It is a tool that takes a complete snapshots of your work with each commit (“save”) thereby allowing the user to roll an entire project forward and aft in time without having to worry about piecing the project back together.
Git can be downloaded for windows in a form of a linux emulator (GitBash), whilst mac and linux distributions may already have it installed. The user can work with it 100% offline if that is desired, however cloud providers such as GitHub and GitLab offer a collaborative online experience for a team of developers.
2.22.1. Setup¶
Set up your username in the terminal
git config --global user.name "Viktor Kis"Set your email address:
git config --global user.email name@example.comSet your editor:
git config --global core.editor vimCheck your user setttings:
git config --list
2.22.2. Step-by-Step GitHub Repo¶
Create a free github account at: github.com
On the top right click your
user iconand a drop-down should appear > ClickYour repositoriesOn your
Repositoriestab towards the right, there should a greenNewicon, click it.Give your new repo a name and hit
Create repositoryGo back to the
Repositoriestab and select your new repo. On your repo page, towards the right there there should be a greenClone or downloadbutton, click it and copy the http link.On your local machine, open up a terminal and type
git clone http-for-your-repoYou now have a successful link up to your online repo from your local. Let’s step through a basic upload
7.1) Add a new file to your repo folder (inside your project folder that was cloned down)
7.2) Add it to queue:
git add .7.3) Add a commit message:
git commit -m "initial demo upload"7.4) Push it to the github:
git push
2.22.3. Commands¶
2.22.3.1. General¶
To initialize a folder:
git initor clone an existing repogit clone urlNote that
git clone urlsets up your remote link,git initdoes not (see remote below to link up a initialized project)
To add file(s) in queue for save: all files
git add .single filegit add filenameTo remove an already added file from queue:
git resetTo commit a change:
git commit -m "msg with your commit"To push a commit to the cloud:
git pushTo pull the latest data from the branch:
git pullor explicitlygit pull origin master(note thatgit fetchworks similarly, however it does not merge the work with your local changes)To completely overwrite local files with server files:
git reset --hard origin/master
2.22.3.2. Status¶
To check change status:
git statusTo check the past commit logs:
git log --graphto limit the loggit log --since=2.weeks
2.22.3.3. Branches¶
To create a new branch:
git checkout -b branchnameTo switch between branches:
git checkout branchnameTo merge a branch onto another:
git merge
2.22.3.4. Ignore Files¶
To create an ignore files/file-types, create a .gitignore:
touch .gitignoreBy practice developers should only commit source files (no binaries, no .pyc files, no config files and etc.) ex;
*.pyc
2.22.3.5. Remote¶
To add a remote link
git remote add user_defined_remote_name url
git clone github.com/project.git
git remote -v
>>> origin https://github.com/project.git (fetch)
>>> origin https://github.com/project.git (push)
# to add a link to someone's fork of the project for example, we would:
git remote add fork_vik https://github.com/vik/project.git
git remote -v
>>> origin https://github.com/project.git (fetch)
>>> origin https://github.com/project.git (push)
>>> fork_vik https://github.com/vik/project.git (fetch)
>>> fork_vik https://github.com/vik/project.git (push)
# now we have to option to pull/fetch from a fork onto our local project
git pull fork_vik master
2.22.3.6. Branch¶
To create a branch:
git branch branch_nameTo work/change your current branch:
git checkout branch_name
2.22.4. Common Issues¶
“failed to push some refs to repo, tip of your current branch is behind”
Cause: there were changes to the remote repo that you dont have (this could be file or history log change)
Fix: run a
git pulland resolve the conflicts