Linux
Git generally comes pre-installed with Linux. You can find out by typing
git status
If you get a ‘fatal: Not a git repository (or any of the parent directories): .git’, you have git.
If you get something like ‘bash: git: command not recognized’ then go to
https://git-scm.com/download/linux
And follow the relevant instructions to set up git.
Mac
Install homebrew first, and then type
brew install git
Open the terminal, and type in (with the quotes)
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
If you do not have an SSH key, “No such file or directory” will pop up. If so, type in
ssh-keygen -t rsa -C "your_email@example.com"
and accept the defaults by pressing ENTER.
Next, type in
cat ~/.ssh/id_rsa.pub
You will see a very large hash key. That is your SSH key and one way git can verify that it is indeed you.
Go to your git account > Settings > SSH and GPG Keys > New SSH Key
Paste that ssh key and give it a title that tells you which machine it is, for example “CSE Machine”, “personal-mac”, etc.
Git knows what you did last summer.
Git, as you may have heard, is a version control system.
In the company that you work for, the software engineers (and that includes yourself) have worked hard to get a minimum viable product running, and it was a huge success. After tasting success in version 1.0 of your product, you now want to add more features to it when version 2.0 comes out. And thus you delve into your mugs of coffee and work even harder to add the features. Version 2.0 is now ready and you release it.
As it turns out, version 2.0 has some serious bugs in it that somehow passed your tests and is now running in production. You guys now need to roll back to version 1.0, which is a stable version and is yet to produce any serious bugs.
Such a scenario is quite common in any place where software is used, and thanks to git, we can roll back to a stable version of our choice and keep working on fixing the bugs before releasing it.
Create a new repository in your remote git account (the online github/bitbucket account) . For now, let’s go to github.com and login. Then from your dashboard, locate the button “New Repository”. Fill in the name and description fields and create the repository as private.
Initialize local repository
Initialize a git repository in your local machine. First create a folder for your new local repository.
mkdir proj_name
cd proj_name
Now, type the following command to initialise an empty repository.
git init
Running the command should display the following message:
Initialized empty Git repository in /home/path/to/project/.git/
If you run, ls -a
, you should be able to see a new .git
folder in the current project directory. Git stores all the tracking information in that folder.
Let’s add a ReadMe file so we have something to work with. Open a ReadMe file and write “Starter ReadMe File”
touch ReadMe.txt
to create a file named ReadMe.txt
initial edit
git add ReadMe.txt
git commit -m "Added a new read me file"
Type git log
to check your commits. You should be able to see your last commit. As you add features in your project or fix bugs, add and commit your changes. This will allow you to go back to your working code if your current code doesn’t work as intended.
Initialize remote repository and add to local
If you want to work on a group project, you’ll need to collaborate with your friends. This is where centralised repository hosting services like GitHub or BitBucket come into play. They provide version control and source code management based on Git and add extra features to assist the development cycle. While Git is a command line tool, these separate online services provide a web-based graphical interface and several collaboration features such as task management tools. In this course, we will be using GitHub.
Before you start working on GitHub, make sure you have everything setup first. There are instructions for getting started at the top of this week’s lab.
Now, create a new repository in your remote git account (GitHub account in this case) so that you can link it with your local repository. To do this, go to github.com and login. Then from your dashboard, locate the button “New Repository”. Fill in the name and description fields and create the repository as private. This online GitHub repository will now be referred to as your remote project.
In your remote project, you will see a button “Clone or download”. Click on it, click on “Use SSH”.
Copy that url. It will typically look like this:
git@github.com:Your_id/Repository_name.git
git remote add origin remote_repository_url
git remote -v
(to verify)
Now, let’s push the ReadMe file that we added before to Github so that everyone can see it.
git push -u origin master
And you have made your first push. Now let us add some code to it so that we can see the actual functionalities of git.
Cloning
Cloning an existing repository is fairly straightforward. Go to the website, and find the green ‘Clone or Download’ button. Click on it and copy the clone url, which looks likehttps://github.com/username/repository.git
After that, open up a terminal and type
git clone <remote_repository_url>
The repository will be downloaded into your local machine.
Committing and Adding
Let’s add a very simple minimal Hello World application. For this, create a file named “hello_word.py” and inside, write
print("Hello, World!")
Save the file and do the following
git add .
(Stage the directory)
git commit -m “Added a minimal application”
(Add a commit message)
git push
(Push to remote)
If you look into your remote git account, you should be able to see the new file added
Branching out
If you are to collaborate on a project, or you want to add a new feature but don’t want to mess up the old features, you would want to create a new branch and make your changes.
To create a branch, type in
git checkout -b new-branch-name
Note that the -b flag is needed only when creating a branch. After it is created, you can just switch into it by typing in
git checkout new-branch-name
Now, make some changes to your file. For instance, add a comment like “A change exclusive to this branch only”.
git add .
git commit -m “Added a comment in this branch”
git push
Notice that it will show that everything is up to date, even though you had made changes. That is because your new branch exists only locally, and not remotely. Not yet anyway. So by default git tried to push local master to remote master. In order to tackle that, do
git push --set-upstream origin new-branch-name
or
git push -u origin new_branch_name
(Note that this is only needed when you are pushing for the first time from a branch. After that, git push will suffice).
To notice the differences, type in
git diff master
At any point, to check which branch you are on, type in
git branch
To check changes that you are yet to commit, type in
git status
Merge conflict
A git pull a day keeps the conflicts away.
Merge conflicts happen when two people try to push together (not necessarily at the same time, but after the same pull) and they have different code in certain parts of the code, then git doesn’t know which change to accept, so it throws merge conflicts. The larger the project is, the harder it is to fix. Let us look at an example.
By default, all git repositories have the branch ‘master’. Let us make another branch called dev
.
Before making the branch dev
, open a hello-world.py file and put in
print(“This is a git branch”)
Add, commit and push.
Then run
git checkout -b dev
This will create a new dev
branch and move you to the new branch. If you do git branch
now, you’ll notice that your current branch is dev
. While in dev, underneath line 1, add the following line
“This is branch dev”
Add, commit and push to origin dev
.
Go back to master
by running git checkout master
and just as before, add the line
“This is branch master”
Add, commit and push to origin master
.
Now, type in the following commands
git merge dev
This should bring up a merge conflict message. In order to fix your merge conflicts, go to your file and locate the ‘<<<<<<<’ and ‘>>>>>>>’ and pick the one that you think is appropriate. You can even keep both if you wish. Remember to remove the merge conflict markers when you update the file.
Add, commit with the message “Fixed merge conflict from branch dev” and push to origin master
.
There are several other ways to get merge conflicts.
In order to avoid merge conflicts, make sure you don’t leave uncommited changes at the end of the day and most importantly, at the start of the day, do a git pull.