Git and Github

Version control System

Git

Git is used to maintain different version of their code in order to keep track of their projects and make sure sure everything is organized and that they are able to effectively collaborate.

What does Git do for you?

1. Keep track of changes to code:

2. Synchronizes code between different people: Often times, you're not the only person who code. So a version control system synchronizes code between different people and making sure everyone has the access to the same code base.

3. Test changes to your code without losing the original:

4. Revert back to old versions of code.

Github

‚ÄčGithub is just a website (or a service) who purpose in life is to store Git repositories on the Internet.

Create a repo on Github:

1. Git clone: It downloads the Github repo to your local computer.

git clone https://github.com/hacback17/cs50-again-learning-git.git
cloning a directory

I have added a file named hello.html to the repository.

To push the file to the Github. We need another command git add.

Git add

git add takes one or more number of files that we have changed and tell git that these are the files that we want to include the next time we make a commit, the next time we take a snapshot of the repository.

Adding a file to Git

Git commit

git commit -m "message" is just a fancy way of saying take a snapshot of the repository in the current moment and save it.

Git add and commit commands in one command: git commit -am "some message"

This both adds the code and commits at the same time.

git commit -a automatically stage all tracked, modified files before the commit

-m for message

Git status

It is an easy way to seeing what's going on in the repository: git status

Git push

git push uploads your local files and directories to the repository that is stored somewhere on the Internet.

pushing the files to the repo

At this point, you may be asked to set up your Git with your name and email address and log-in to your Github account if you haven't already done so.

Now the same copy of my hello.html is stored on Github as well.

Git pull

If someone (in your team) has made a change in your code and you want to download the recent change, use git pull.

Merge Conflicts:

In some cases, when you and the other person has made changes to the same lines of the same files, you may run into a merge conflict.

So the error suggests you to pull down the changes made to the remote repo, remove what you need and don't need in the code and push again.

So finally we resolve the merge conflict.

Git log

History of all the different commits that have been made.

Note:

  • Each log consists of at least a commit hash, author who made the commit, date and time and the message.

  • You can use the commit hash to revert to a previous version of code.

Revert to old code

  • git reset --hard <commit hash>

  • git reset --hard origin/master

Assume that I want to revert to the my first change: copy the the first few characters of the any hash (whichever change you want to see reflected) and use the command.

Here is an older version of the code.

Branching

Rather than following a linear progression of entire project where each change follows the change immediately before it, branching lets us take our project in multiple direction.

Imagine that you and your team-mate working on a new feature in the web app, you can use separate branch for that feature and once it completes add that to your master branch.

Branching

Head is a Git terminology which indicates where it is currently pointing at (which branch code your are looking at). However, you can easily change the Head when required.

Changing the Head

Once you are ready with the new feature and wish to add that to the existing web app, you are merge them:

Merging new feature to the master branch

git branch: check all the branches available:

List of all the branches

git branch new-branch-name: adds a new branch in the code:

Adding a new feature in the new branch.

So when you checkout (moving to a new branch) the new branch, you only see what changes you had made and committed in that branch before.

merging the branch