Git Basics
Posted May 28, 2023
By Kevin Schwickrath3 min read
What is Git?
Git is a Source Code Management (SCM) or version control tool. It is usually used for coordinating work among several programmers for developing software. To learn lots of neat things Git can do check out the reference material here: git - Documentation
Installing Git
Git comes installed on most *nix based systems. I can’t think of any major distro that doesn’t have it by default. Maybe Biebian, I don’t know, never tried it.
For Windows you can grab the installer here: https://git-scm.com/download/win.
Setup Git
Git needs to be setup before you can use it. This is used to track who made changes.
git config --global user.name "GlobalUser"git config --global user.email "GlobalUser@youremail.com"If you want to use specific information for each project you can use the following within each repo:
git config user.name "ProjectUser"git config user.email "ProjectUser@youremail.com"Repository Terms
- repo = repository
- Local repo - a local repository on your computer used to work on the local version of a project.
- Remote repo - a repo that is usually stored on another system or server. This is often used when multiple people want to work with a project’s code or you want to move the code to another place, like GitHub.
Initialize your local git repo
git initgit initInitialized empty Git repository in D:/Dropbox/wwtw/Live/.git/Check the status of the new repo
git statusgit statusOn branch master
No commits yet
Untracked files: (use "git add <file>..." to include in what will be committed) index.htmlTracking and Commiting
Tracking With Git
Git has two step process for managing files. First you add files to staging. This allows git to track them. Then you commit them. Commiting is recording the changes to the repo.
A single file to staging
git add index.htmlMultiple specific files
git add index.html index2.htmlAll files in directory
git add .All folders and files in this directory
git add --allAll folders and files of a specific directory
git add subdirGit Commit
Here we commit the changes we tracked above and add a message with the action.
git commit -m "Created repo"Commit History
This command will give you the commit history messages and the “hash” of the commit.
git logBranches
Git works like a timeline, think Back to the Future II. Git calls these branches. By default, in a new repo, you are placed in the branch master.
You can see this via git status.
git statusOn branch masterOr with git branch, this will show you the branches and the one with the star will show you what branch you’re on.
git branchTo split your project to a new branch at the current point use git branch branch-name. This is useful if you want to test some changes and play around without impacting the main code.
git branch <branch-name>Rolling back
One of the greatest features of git is the ability to roll back to a previous version of your code.
git checkout <commit-hash>To go back to the latest version of your code use
git checkout masterTo rollback your code to a previous version and make changes you need to split the changes to a new branch. This can be done with -b. This is helpful if you want to experiment with a bug fix but not change the code for everyone.
git checkout <commit-hash>(make changes)git checkout -b <new-branch-name>You can swap to another branch any time with
git checkout <branch-name>Merging branches
If you like the changes and wish to combine one branch with another use git merge branch-name. To use this command change to the branch you wish to merge the changes into. For example, if you want to update master with the changes in test-branch-name use the following:
git checkout mastergit merge test-branch-nameThis moves us from whatever branch we were in to master. Then we merge the changes with test-branch-name into the branch we’re in, master.
Putting this all together in an example
git branch 1955-biffgit checkout 1955-biffgit merge 1955-biffgit checkout mastergit merge 1955-original- I create 1955-biff for when he travels back in time
- Then check it out to use it an make changes
- Merge the 1955 Biff created with the original 1955 overwriting the original
- I then checkout master
- Merge the 1955 changes with master so everyone is now using Biff’s self created future
Enjoy your versions of things.
This post is licensed under CC BY 4.0 by the author.
Related Posts
Deploy a Project to Your Server with Git
Create the directory for the git repo.