To use git, there is no specific language or framework, it just stores any file.
Features
- Distributed version control (decentralized). It enables many developers to work on the same project without being on the same network, leave alone the same physical location.
- Coordinates work between multiple developers.
- Who made what changes and when. Git tracks every single version and change made on the project as long as it was committed to the repository.
- Ability to revert back at any time.
- Links local and remote repositories. You do not need internet connection to work on the local repository unless you want to push changes to the remote repository.
- Git keeps track of code history.
- Takes “snapshots” of your files.
- Makes you decide when to take a snapshot by making a “commit”.
- You can visit any snapshot at any time.
- You can stage files before committing.
Next, we need to create an account on Github, and then configure our username and password on git.
To start using Git and Github, we need to sign in to Github by using our username/email address and password we registered with, and create a repository, e.g., gitdemo.
To easily keep track of changes on both Desktop and github.com and associate our local directory to our Github remote repository, we will create a directory with the same name as the repository on Github on our Linux distribution (e.g., Ubuntu) Home folder. The folder can however be anywhere, e.g., on the Desktop).
Basic git commands
We have four steps that work with git: Working directory, staging/index area, local repository and remote repository.
Note that all the demonstrations will be reflected at
TSSFL/gitdemo
Contribute to gitdemo development by creating an account on GitHub.- #Initialize local git repository (This creates .git file in the directory):
- $git init
- #Add file named filename to the staging area or index to make it ready for commit:
- $git add filename
- #Check status of working tree:
- $git status
- #Commit changes (take whatever from index/staging area to the local repository):
- $git commit #This has a number of options, we will see later
- #Push changes in the local repository to the remote repository:
- $git push #We will need credentials or add ssh keys to avoid using credentials
- #Pull the latest changes from the remote repository:
- $git pull
- #Clone a remote repository/directory into the current local directory:
- $git clone ../remote_repo.git.
- #Get help from git:
- $git help <verb>
- $git <verb> --help
- #Stop tracking working directory with git:
- $git rm -rf .git
- #Check files present in the working directory:
- $ls -la
- #Don't panic, stay calm because we will work all the above basic commands by using real examples in a meanwhile.
- #Installing git and configuring credentials – username and email address on git
- #Install git:
- $sudo yum install git #Fedora
- $sudo apt-get install git #(Debian, Ubuntu, Linux-Mint)
- #Add user name and email address:
- $git config --global user.name “username” #Put between “ “ the username you registered with on Github
- $git config --global user.email “user email add” #Put between “ “ the email address you registered with on Github
- #List all our configuration details (in this case our username is tssfl):
- $git config --list
- user.name=tssfl
- user.email=user@tssfl.com
- core.repositoryformatversion=0
- core.filemode=true
- core.bare=false
- core.logallrefupdates=true
- remote.origin.url=https://github.com/tssfl/gitdemo.git
- remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
- branch.master.remote=origin
- branch.master.merge=refs/heads/master
- Working with Git and Github – Real Examples
- #Make the directory gitdemo, move into it and initialize git
- $mkdir gitdemo
- $cd gitdemo
- $git init
- #Let's create a file named demo.cpp and write in it few lines of code:
- $git touch demo.cpp #Write in it
- #Let's add this file to our local repository:
- $git add demo.cpp
- #Untrack the file (untracked files are files not yet added to the local repository)
- $git rm --cached filename #For windows
- $git reset demo.cpp
- $git reset #Resets everything
- #Add any file with extension .cpp to the staging area:
- $git add *.cpp
- #Add all files from the working directory to the staging/index area:
- $git add -A #Or
- $git add .
- #Add demo.cpp to the local repository with a comment “Added the C++ Hello World”
- $git commit -m “Added the C++ Hello World”
- #Get the hashes corresponding to that commit:
- $git log #All hashes are unique
- #Create .gitignore to add files and folders we don't want to include in our repository
- #Create and add to .gitignore log.txt and errors.txt files:
- $touch log.txt errors.txt
- $git add . #log.txt and errors.txt cannot be staged
- #Commit changes:
- $git commit -m “Some changes”
- #Link remote repository with local repository:
- $git remote add origin https://github.com/TSSFL/gitdemo
- #fatal: remote origin already exists. - You will get this message if the local directory and remote repository are already linked
- #Push changes from local repository to remote repository:
- $git push -u origin master # -u links the local directory to remote repository
- Username for 'https://github.com':
- Password for 'https://tssfl@github.com':
- #You should see at https://github.com/TSSFL/gitdemo, three files named .gitignore, demo and demo.cpp after pushing changes.
- Branches
- #Branches enable you to work separately on specific pieces/parts/ of a code or separate codes until they are matured enough for integration to the master branch/workflow. #Regard branch in git as a temporary working space.
- #Create a branch named new_branch
- $git branch new_branch
- #Switch from master branch to new_branch
- $git checkout new_branch #Switched to branch 'new_branch'
- #Create a new C++ file, named demo2.cpp and write in it
- $touch demo2.cpp
- -#Add demo2.cpp from the working directory to the staging/index area
- $git add demo2.cpp
- #Add (commit) demo2.cpp to the local repository with a comment “Added git usage demo2”
- $git commit -m “Added git usage demo2”
- #Check status:
- $git check status
- #Switch back to the master branch:
- $git checkout master #Switched to branch 'master'
- . Your branch is up-to-date with 'origin/master'.
- $git status #demo2.cpp is in new_branch
- #Merge new_branch to the master branch (do this while in master branch):
- $git merge new_branch
- #Probe remote repository:
- $git remote
- origin
- #Connect your local repository to a remote server, add the server to be able to push to it:
- $git remote add origin https://github.com/TSSFL/gitdemo.git
- fatal: remote origin already exists.
- #Push changes from local repository to remote repository:
- $git push -u origin master
- Username for 'https://github.com':
- Password for 'https://tssfl@github.com': #Branch master set up to track remote branch master from origin.
- Quick Example
- #Create another branch named another_branch
- $git branch another_branch
- #Switch from master branch to another_branch
- $git checkout another_branch #Switched to branch 'another_branch'
- #Create README.md file:
- $touch README.md
- #Stage README.md:
- $git add README.md #Or simply
- $git add .
- #Commit README.md:
- $git commit -m “Added Read me file” #File committed locally
- #Merge another_branch to master branch:
- $git checkout master
- $git merge another_branch
- #Push changes to remote repository master:
- $git push origin master
- Username for 'https://github.com': tssfl
- Password for 'https://tssfl@github.com':
- #Whenever your are working on a project that potentially have multiple developers, use
- $git pull origin master
- #before pushing changes to pull changes that might have been made by other developers since the last time you pulled from the repository
- #After pulling from origin, check differences between the files in the local directory and remote repository
- #View all the merge conflicts:
- $git diff
- #View the conflicts against the base file:
- $git diff --base filename
- #Preview changes, before merging:
- $git diff sourcebranch targetbranch
- #You will then have to proceed committing and pushing changes, like we did previously:
- $git status
- $git add -A
- $git commit -m “modified a couple of things”
- #Check if the local and remote repositories are the same because there might be changes made by others which you need to consider before pushing your changes
- $git pull origin master
- * branch master -> FETCH_HEAD
- Already up-to-date.
- $git push -u origin master #Or
- $git push
- ..
- ..
- Branch master set up to track remote branch master from origin.
- Remote/local repositories
- #View information about remote repository:
- $git remote -v
- #List branches on local and remote repositories
- $git branch -a
- A common workflow for most developers
- #Create branch and start working on that branch
- $git branch current_branch
- #Start working on current_branch (switching from master branch to current_branch)
- $git checkout current_branch #Switched to branch 'current_branch'
- #Modify some file, e.g., demo.cpp, and then
- $git status
- #Stage changes
- $git add .
- #Commit changes to the local repository:
- $git commit -m “Modified Hello World”
- #Push current_branch to remote repository:
- $git push -u origin current_branch #-u option associates the local and remote current_branch, git remembers them and next time we can use git push and git pull
- #Check branches
- $git branch -a
- #Merge a branch with master (after you have tested everything works very well)
- #Switch to master branch
- $git checkout master
- #Pull out master branch to check out if any changes were made:
- $git pull origin master
- #Find out if there are any branches we have merged to master
- $git branch --merged
- #Merge current_branch to master
- $git merge current_branch
- #Push changes to remote master branches:
- $git push origin master
- Delete the branches
- #Find out merged branches:
- $git branch –merged
- #Delete all branches that have been merged to master branch:
- $git branch -d another_branch new_branch
- #Check which branches are available locally and remotely:
- $git branch -a
- #Delete all remote branches merged to master branch
- $git push origin --delete another_branch
- Username for 'https://github.com': tssfl
- Password for 'https://tssfl@github.com':
- #You will get this message if the branch does not exist remotely:
- error: unable to delete 'another_branch': remote ref does not exist
- error: failed to push some refs to 'https://github.com/TSSFL/gitdemo'
- Example
- #Create a new branch named print:
- $git branch print
- #Switch from master branch to print branch:
- $git checkout print
- #Open any file in the working directory and edit, e.g.,
- $gedit README.me #Edit, save and quit
- #Check status:
- $git status #Must show a modified unstaged file
- #Let's add this file to staging directory:
- $git add README.me
- #Commit changes to local repository:
- $git commit -m “Modified the README.me once more”
- #Push the print branch to origin/remote repository:
- $git push -u origin print
- Username for 'https://github.com': tssfl
- Password for 'https://tssfl@github.com':
- #Switch to master branch:
- $git checkout master
- #Pull all changes made, if any before merging:
- $git pull origin master
- #Merge print branch to master branch:
- $git merge print
- #Push changes to remote repository:
- $git push origin master
- Delete print branch
- #Find out merged branches:
- $git branch --merged
- #Delete all branches that have been merged to master branch, the only remote branch is print:
- $git branch -d print
- #Check which branches are available locally and remotely:
- $git branch -a
- #Delete all remote branches merged to master branch
- $git push origin --delete print
- Username for 'https://github.com': tssfl
- Password for 'https://tssfl@github.com':
- To https://github.com/TSSFL/gitdemo
- - [deleted] print
- Further commands
- #Commit any files you've added with git add, and also commit any files you've changed since then:
- $git commit -a
- #Push all branches to your remote repository:
- $git push --all origin
- #Force your local directory revision to the remote repository by using:
- $git push -f origin master
- #Instead of dropping all your local changes and commits, fetch the latest history from the server and point your local master branch at it:
- $git fetch origin
- $git reset --hard origin/master
- #Download (create a working copy of the remote repository) the repository from Github by cloning:
- $git clone https://github.com/TSSFL/gitdemo.git path_to_destination_directory (without path destination will default to Home folder)
- #E.g.,
- $ git clone https://github.com/TSSFL/gitdemo.git ./Desktop/git_dir
- #For a remote server, use:
- $git clone username@host:/path/to/repository
Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each time you make some operations that require authentication such as pushing changes:

How to generate ssh key for git
How to generate ssh key for githttps://help.github.com/articles/connec ... -with-ssh/
Possible error:
Code: Select all
git error after git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Code: Select all
$git remote add origin https://github.com/TSSFL/gitdemo
Previously on Git and Github
Here are few gentle steps on how to install and test Git. We chose Linux-based distribution for testing.
Introduction to GitHub and Git installation procedure can be found at
What is GitHub?
GitHub is a code hosting platform for version control and collaboration. It enables you and others work together on projects from anywhere, see more https: activities hello-world...although the easiest and direct way to install Git on Linux-based distribution such as Ubuntu would be via a command line:
Code: Select all
sudo apt-get install git #Install Git
Code: Select all
git config --global user.name "username" #username should be identical to the one provided on GitHub
Code: Select all
git config --global user.email "emailaddress" #Use the same email address provided on GitHub
Create directory with the same name as that on GitHub on your Linux distribution Home folder (e.g., Ubuntu). (The folder can be anywhere, e.g., on your Desktop)
Code: Select all
mkdir GitDemo
Now, test how to use Git and GitHub
- cd GitDemo #Move to the directory GitDemo
- echo "# GitDemo" >> README.md #Create README.md file inside GitDemo
- git init #Initialize Git in GitDemo directory
- git add README.md #Add the file README.md to the Git
- git commit -m "first commit" #Commit the file you have added to the Git, it is like saving changes to the Git, " " is for comments
- git remote add origin https://github.com/username/GitDemo.git #Link the local directory GitDemo to the remote one on GitHub
- git push -u origin master #Commit/save changes in the GitHub directory. You need to provide login credentials: username and password
- nano newfile.txt #We create another file in GitDemo directory, write something in it, e.g., "Hello git", press Ctrl+O, and then Enter to save changes, press Ctrl+x to exit. You can use any editor instead of nano
- git add newfile.txt #Add newfile.txt to the Git
- git commit -m "I added newfile.txt to the Git" #Save changes to the Git
- git push -u origin master #Push/save changes in the GitHub Repository. For https://, you need to provide username and password