This is a basic walk through of getting a project under version control using Git, from the installation of git, to initializing a project, adding and committing files to pushing to a remote repository. As well as cloning an existing project to start working with.
Head to https://git-scm.com/downloads here you’ll be be able to download the version of Git for your OS. Here I’m using windows. Download and run the executable, On the “Select Components” screen I select “Addidtional icons – On the Desktop”. From there on it’s pretty much next next…finish and let it install.
The first step is to do some basic configuration such as configuring your name and email. This will show the identity of the developer who added all that amazing code to the project.
# set git config --global user.name "james mcneil" git config --global user.email "firstname.lastname@example.org" # Check you settings git config --list
Create a new repository:
Create a new directory, move into the directory and perform “git init”
$ mkdir myNewApp $ cd myNewApp/ $ git init Initialized empty Git repository in C:/Users/mcnei/myNewApp/.git/ mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $
Working local there are 3 area’s maintained by git, the working directory, the staging area (index) and the repository (.git directory), also known as HEAD (the last commit made).
Create a new file inside the your project directory (working directory). Checking on the status you see that the new file is listed as untracked. Next we add the untracked file into the staging area. Then finally we need to commit any changes into the local repository (HEAD).
git status is a handy command to use to see what state our working directory is in compared to our repository.
# Create a file $ echo "This is the contents of the file new file" > FileOne.txt # Check status $ git status On branch master No commits yet Untracked files: (use "git add ..." to include in what will be committed) FileOne.txt nothing added to commit but untracked files present (use "git add" to track) # Add the untracked file to the staging area $ git add FileOne.txt warning: LF will be replaced by CRLF in FileOne.txt. The file will have its original line endings in your working directory. # Checking status again (File showing there are changes to be committed) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: FileOne.txt # Committing changes to repository (always put in a good descriptive commit message) $ git commit -m "Initial commit" [master (root-commit) 3796123] Initial commit 1 file changed, 1 insertion(+) create mode 100644 FileOne.txt # Again just checking the state $ git status On branch master nothing to commit, working tree clean mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $
As you work through your project is always good to check the status of the working directory and repository with git status. After making changes to any files in the project you will have to stage the changes to the with git add. Then commit them to the repository.
$ git status On branch master Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: FileOne.txt no changes added to commit (use "git add" and/or "git commit -a") mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $ git add . warning: LF will be replaced by CRLF in FileOne.txt. The file will have its original line endings in your working directory. mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $ git commit -m "Add the next line of code" [master 9490587] Add the next line of code 1 file changed, 1 insertion(+) mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $ git status On branch master nothing to commit, working tree clean
Next we’ll configure a remote repository on GitHub.com and push our local repository to it. Making it available for others working on the project to make a clone locally on their computer to continue with it’s development.
# push our existing repository from the command line git remote add origin https://github.com/JAYMAC1/myApp.git # the -u tells git to remember the parameters. $ git push -u origin master Counting objects: 6, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (6/6), 526 bytes | 131.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0) To https://github.com/JAYMAC1/myApp.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
When you go start working with your project at a later date it’s always best to pull from the remote repository in case any changes have been made to the code by using the git pull command. Below you can see that a README.md file had been created in the projects remote repository. By running the git pull the new file (and any changes) are pulled to our local repository.
$ ls FileOne.txt mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $ git pull remote: Counting objects: 3, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/JAYMAC1/myApp 9490587..e484df6 master -> origin/master Updating 9490587..e484df6 Fast-forward README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md mcnei@lab-pc3 MINGW64 ~/myNewApp (master) $ ls FileOne.txt README.md
Clone an existing repo:
If your looking to start working with an existing repository from a repository on Github, Gitlab or any other remote system. You can use the git clone command to make a full copy of the repo.
$ git clone https://github.com/JAYMAC1/myApp.git Cloning into 'myApp'... remote: Counting objects: 9, done. remote: Compressing objects: 100% (6/6), done. remote: Total 9 (delta 0), reused 6 (delta 0), pack-reused 0 Unpacking objects: 100% (9/9), done. mcnei@lab-pc3 MINGW64 ~/projects $ ls myApp/ mcnei@lab-pc3 MINGW64 ~/projects $ cd myApp/ mcnei@lab-pc3 MINGW64 ~/projects/myApp (master) $ ls FileOne.txt README.md
This was a very simple introduction to get up and running with Git an SCM system. If you’re writing code or config files using SCM should be part of your workflow. There are amazing things that can be done with code from a SCM system, such as making commits part of CI/CD pipeline to automate testing and deployment of code into test and then production infrastructures. It can be used to track issues and collaboration can be extended by using webhooks and integrating it with applications like Slack or Mattermost. I’ll add other posts with tips on how I use it in my day to day work.