Git Installation, Configuration and Testing with Github

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Git is a version control system (VCS) for tracking changes in computer files. Initially created in 2005 by Linus Torvalds who is also a creator of Linux kernel.

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.
Concepts
  • 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.
To be able to use Git and Github we need to install git in our machine.

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 https://github.com/TSSFL/gitdemo
  1. #Initialize local git repository (This creates .git file in the directory):
  2. $git init
  3.  
  4. #Add file named filename to the staging area or index to make it ready for commit:
  5. $git add filename
  6.  
  7. #Check status of working tree:
  8. $git status
  9.  
  10. #Commit changes (take whatever from index/staging area to the local repository):
  11. $git commit #This has a number of options, we will see later
  12.  
  13. #Push changes in the local repository to the remote repository:
  14. $git push #We will need credentials or add ssh keys to avoid using credentials
  15.  
  16. #Pull the latest changes from the remote repository:
  17. $git pull
  18.  
  19. #Clone a remote repository/directory into the current local directory:
  20. $git clone ../remote_repo.git.
  21.  
  22. #Get help from git:
  23. $git help <verb>
  24. $git <verb> --help
  25.  
  26. #Stop tracking working directory with git:
  27. $git rm -rf .git
  28.  
  29. #Check files present in the working directory:
  30. $ls -la
  31.  
  32. #Don't panic, stay calm because we will work all the above basic commands by using real examples in a meanwhile.
  33.  
  34. #Installing git and configuring credentials – username and email address on git
  35.  
  36. #Install git:
  37. $sudo yum install git #Fedora
  38. $sudo apt-get install git #(Debian, Ubuntu, Linux-Mint)
  39.  
  40. #Add user name and email address:
  41. $git config --global user.name “username”  #Put between “ “ the username you registered with on Github
  42. $git config --global user.email “user email add”  #Put between “ “ the email address you registered with on Github
  43.  
  44. #List all our configuration details (in this case our username is tssfl):
  45. $git config --list
  46. user.name=tssfl
  47. user.email=user@tssfl.com
  48. core.repositoryformatversion=0
  49. core.filemode=true
  50. core.bare=false
  51. core.logallrefupdates=true
  52.  remote.origin.url=https://github.com/tssfl/gitdemo.git
  53. remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
  54. branch.master.remote=origin
  55. branch.master.merge=refs/heads/master
  56.  
  57. Working with Git and Github – Real Examples
  58.  
  59. #Make the directory gitdemo, move into it and initialize git
  60. $mkdir gitdemo
  61. $cd gitdemo
  62. $git init
  63.  
  64. #Let's create a file named demo.cpp and write in it few lines of code:
  65. $git touch demo.cpp #Write in it
  66.  
  67. #Let's add this file to our local repository:
  68. $git add demo.cpp
  69.  
  70. #Untrack the file (untracked files are files not yet added to the local repository)
  71. $git rm --cached filename  #For windows
  72. $git reset demo.cpp
  73. $git reset #Resets everything
  74.  
  75. #Add any file with extension .cpp to the staging area:
  76. $git add *.cpp
  77.  
  78. #Add all files from the working directory to the staging/index area:
  79. $git add -A #Or
  80. $git add .
  81.  
  82. #Add demo.cpp to the local repository with a comment “Added the C++ Hello World”
  83. $git commit -m “Added the C++ Hello World”
  84.  
  85. #Get the hashes corresponding to that commit:
  86. $git log #All hashes are unique
  87.  
  88. #Create .gitignore to add files and folders we don't want to include in our repository
  89. #Create and add to .gitignore log.txt and errors.txt files:
  90. $touch log.txt errors.txt
  91. $git add . #log.txt and errors.txt cannot be staged
  92.  
  93. #Commit changes:
  94. $git commit -m “Some changes”
  95.  
  96. #Link remote repository with local repository:
  97. $git remote add origin https://github.com/TSSFL/gitdemo
  98. #fatal: remote origin already exists. - You will get this message if the local directory and remote repository are already linked
  99.  
  100. #Push changes from local repository to remote repository:
  101. $git push -u origin master # -u links the local directory to remote repository
  102. Username for 'https://github.com':
  103. Password for 'https://tssfl@github.com':
  104.  
  105. #You should see at https://github.com/TSSFL/gitdemo, three files named .gitignore, demo and demo.cpp after pushing changes.
  106.  
  107. Branches
  108.  
  109. #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.
  110.  
  111. #Create a branch named new_branch
  112. $git branch new_branch
  113.  
  114. #Switch from master branch to new_branch
  115. $git checkout new_branch #Switched to branch 'new_branch'
  116.  
  117. #Create a new C++ file, named demo2.cpp and write in it
  118. $touch demo2.cpp
  119.  
  120. -#Add demo2.cpp from the working directory to the staging/index area
  121. $git add demo2.cpp
  122.  
  123. #Add (commit) demo2.cpp to the local repository with a comment “Added git usage demo2”
  124. $git commit -m “Added git usage demo2”
  125.  
  126. #Check status:
  127. $git check status
  128.  
  129. #Switch back to the master branch:
  130. $git checkout master #Switched to branch 'master'
  131. . Your branch is up-to-date with 'origin/master'.
  132. $git status #demo2.cpp is in new_branch
  133.  
  134. #Merge new_branch to the master branch (do this while in master branch):
  135. $git merge new_branch
  136.  
  137. #Probe remote repository:
  138. $git remote
  139. origin
  140.  
  141. #Connect your local repository to a remote server, add the server to be able to push to it:
  142. $git remote add origin https://github.com/TSSFL/gitdemo.git
  143. fatal: remote origin already exists.
  144.  
  145. #Push changes from local repository to remote repository:
  146. $git push -u origin master
  147. Username for 'https://github.com':
  148. Password for 'https://tssfl@github.com': #Branch master set up to track remote branch master from origin.
  149.  
  150. Quick Example
  151.  
  152. #Create another branch named another_branch
  153. $git branch another_branch
  154.  
  155. #Switch from master branch to another_branch
  156. $git checkout another_branch #Switched to branch 'another_branch'
  157.  
  158. #Create README.md file:
  159. $touch README.md
  160.  
  161. #Stage README.md:
  162. $git add README.md #Or simply
  163. $git add .
  164.  
  165. #Commit README.md:
  166. $git commit -m “Added Read me file#File committed locally
  167.  
  168. #Merge another_branch to master branch:
  169. $git checkout master
  170. $git merge another_branch
  171.  
  172. #Push changes to remote repository master:
  173. $git push origin master
  174. Username for 'https://github.com': tssfl
  175. Password for 'https://tssfl@github.com':
  176.  
  177. #Whenever your are working on a project that potentially have multiple developers, use
  178. $git pull origin master
  179. #before pushing changes to pull changes that might have been made by other developers since the last time you pulled from the repository
  180.  
  181. #After pulling from origin, check differences between the files in the local directory and remote repository
  182.  
  183. #View all the merge conflicts:
  184. $git diff
  185.  
  186. #View the conflicts against the base file:
  187. $git diff --base filename
  188.  
  189. #Preview changes, before merging:
  190. $git diff sourcebranch targetbranch
  191.  
  192. #You will then have to proceed committing and pushing changes, like we did previously:
  193.  
  194. $git status
  195. $git add -A
  196. $git commit -m “modified a couple of things”
  197.  
  198. #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
  199. $git pull origin master
  200. * branch            master     -> FETCH_HEAD
  201. Already up-to-date.
  202. $git push -u origin master #Or
  203. $git push
  204. ..
  205. ..
  206. Branch master set up to track remote branch master from origin.
  207.  
  208. Remote/local repositories
  209.  
  210. #View information about remote repository:
  211. $git remote -v
  212.  
  213. #List branches on local and remote repositories
  214. $git branch -a
  215.  
  216. A common workflow for most developers
  217.  
  218. #Create branch and start working on that branch
  219. $git branch current_branch
  220.  
  221. #Start working on current_branch (switching from master branch to  current_branch)
  222. $git checkout current_branch #Switched to branch 'current_branch'
  223.  
  224. #Modify some file, e.g., demo.cpp, and then
  225. $git status
  226.  
  227. #Stage changes
  228. $git add .
  229.  
  230. #Commit changes to the local repository:
  231. $git commit -m “Modified Hello World”
  232.  
  233. #Push current_branch to remote repository:
  234. $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
  235.  
  236. #Check branches
  237. $git branch -a
  238.  
  239. #Merge a branch with master (after you have tested everything works very well)
  240.  
  241. #Switch to master branch
  242. $git checkout master
  243.  
  244. #Pull out master branch to check out if any changes were made:
  245. $git pull origin master
  246.  
  247. #Find out if there are any branches we have merged to master
  248. $git branch --merged
  249.  
  250. #Merge current_branch to master
  251. $git merge current_branch
  252.  
  253. #Push changes to remote master branches:
  254. $git push origin master
  255.  
  256. Delete the branches
  257.  
  258. #Find out merged branches:
  259. $git branch –merged
  260.  
  261. #Delete all branches that have been merged to master branch:
  262. $git branch -d another_branch new_branch
  263.  
  264. #Check which branches are available locally and remotely:
  265. $git branch -a
  266.  
  267. #Delete all remote branches merged to master branch
  268. $git push origin --delete another_branch
  269. Username for 'https://github.com': tssfl
  270. Password for 'https://tssfl@github.com':
  271.  
  272. #You will get this message if the branch does not exist remotely:
  273.  
  274. error: unable to delete 'another_branch': remote ref does not exist
  275. error: failed to push some refs to 'https://github.com/TSSFL/gitdemo'
  276.  
  277. Example
  278.  
  279. #Create a new branch named print:
  280. $git branch print
  281.  
  282. #Switch from master branch to print branch:
  283. $git checkout print
  284.  
  285. #Open any file in the working directory and edit, e.g.,
  286. $gedit README.me #Edit, save and quit
  287.  
  288. #Check status:
  289. $git status #Must show a modified unstaged file
  290.  
  291. #Let's add this file to staging directory:
  292. $git add README.me
  293.  
  294. #Commit changes to local repository:
  295. $git commit -m “Modified the README.me once more
  296.  
  297. #Push the print branch to origin/remote repository:
  298. $git push -u origin print
  299. Username for 'https://github.com': tssfl
  300. Password for 'https://tssfl@github.com':
  301.  
  302. #Switch to master branch:
  303. $git checkout master
  304.  
  305. #Pull all changes made, if any before merging:
  306. $git pull origin master
  307.  
  308. #Merge print branch to master branch:
  309. $git merge print
  310.  
  311. #Push changes to remote repository:
  312. $git push origin master
  313.  
  314. Delete print branch
  315.  
  316. #Find out merged branches:
  317. $git branch --merged
  318.  
  319. #Delete all branches that have been merged to master branch, the only remote branch is print:
  320. $git branch -d print
  321.  
  322. #Check which branches are available locally and remotely:
  323. $git branch -a
  324.  
  325. #Delete all remote branches merged to master branch
  326. $git push origin --delete print
  327. Username for 'https://github.com': tssfl
  328. Password for 'https://tssfl@github.com':
  329. To https://github.com/TSSFL/gitdemo
  330. - [deleted]         print
  331.  
  332. Further commands
  333.  
  334. #Commit any files you've added with git add, and also commit any files you've changed since then:
  335. $git commit -a
  336.  
  337. #Push all branches to your remote repository:
  338. $git push --all origin
  339.  
  340. #Force your local directory revision to the remote repository by using:
  341. $git push -f origin master
  342.  
  343. #Instead of dropping all your local changes and commits, fetch the latest history from the server and point your local master branch at it:
  344. $git fetch origin
  345. $git reset --hard origin/master
  346.  
  347. #Download (create a working copy of the remote repository) the repository from Github by cloning:
  348. $git clone https://github.com/TSSFL/gitdemo.git path_to_destination_directory (without path destination will default to Home folder)
  349.  
  350. #E.g.,
  351. $ git clone https://github.com/TSSFL/gitdemo.git ./Desktop/git_dir
  352.  
  353. #For a remote server, use:
  354. $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:

http://www.w3docs.com/snippets/git/how- ... r-git.html
https://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.
Fix:

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-4690,

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 
Next, create an account on GitHub and sign in, then configure username on Git:

Code: Select all

git config --global user.name "username" #username should be identical to the one provided on GitHub
Next, configure email:

Code: Select all

git config --global user.email "emailaddress" #Use the same email address provided on GitHub
Go to GitHub.com, create Repository, e.g GitDemo

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

  1. cd GitDemo #Move to the directory GitDemo
  2. echo "# GitDemo" >> README.md #Create README.md file inside GitDemo
  3. git init  #Initialize Git in GitDemo directory
  4. git add README.md #Add the file README.md to the Git
  5. 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
  6. git remote add origin https://github.com/username/GitDemo.git #Link the local directory GitDemo to the remote one on GitHub
  7. git push -u origin master #Commit/save changes in the GitHub directory. You need to provide login credentials: username and password
  8. 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
  9. git add newfile.txt #Add newfile.txt to the Git
  10. git commit -m "I added newfile.txt to the Git" #Save changes to the Git
  11. git push -u origin master #Push/save changes in the GitHub Repository. For https://, you need to provide username and password

1
1 Image
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

Check that the repository you are fetching exists on GitHub.

To find the URL (the URL is case-sensitive) of the local repository, open the command line and type

git remote -v
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

Install git in 2024

On Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

  1. $sudo dnf install git-all


On a Debian-based distribution, such as Ubuntu, try apt:

  1. $sudo apt install git-all

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#4

Install git on Ubuntu in 2023

  1. sudo apt update
  2. sudo apt install git


Check the git version:

  1. git --version

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#5

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “Git, Mercurial, GitHub and Bitbucket”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests