Categories
Uncategorized

Day 2: Git

Finally it is time to learn git. I’ve always heard that it is one of the most important tools to learn as a software developer. So here are my notes.

Basics

Initialize a local git repository and add some source files in it:

cd ProjectDirectory
git init
touch index.html
touch app.js

To add files to the staging area (git commits files which are present in staging area only):

git add index.html

To remove a file from staging area:

git rm --cached index.html

To look at the the status of the local repository:

git status

Status gives information about which files are tracked (added to staging), which are changed and which are untracked(not added to staging).

If a file is modified, git status shows it as modified but shows that the modified version is not staged. Use git add to add the updated file to staging area again.

To commit files to repository:

git commit

Above command opens vi editor. remove the comment from line “initial commit” and exit. The files will be committed. Now if you run git status, it will show a message “Nothing to commit”.

To skip the vi editor step use following command:

git commit -m 'your comment'

There may be some files which we do not want to add to repository. To ignore those files/folders, create a file called ‘.gitignore’ and simply add the names of the files that you want to ignore. Let’s say you want to ignore log.txt, all .jpg files and a folder called dir, then the contents of .gitignore should be following:

log.txt
*.jpg
/dir

That’s all you have to do.

Branches

Let us say that you’re working in a team project and you have been assigned the task to create login page. You don’t want to directly modify the code in master branch until the code is complete, so you can create a login branch and commit changes there. To create a branch and switch the working to it, use following commands:

git branch loginBranch
git checkout loginBranch

Now if you create a new file ‘login.html’, modify already existing file ‘index.html’, stage and commit them, they will be committed to login branch only. If you switch branch to ‘master’ again, login.html will disappear and the changes we made in index.html will not show up.

Now if you have completed the login functionality and want to merge the branch to master, do the following:

git merge loginBranch

It will open vi editor and ask for a comment for why this merge is happening. Add a comment and loginBranch will be merged to master.

Working with remote repositories

Add a new repository to your github account using web browser. Use following command for adding a new origin:

git remote add origin 'link to private repository'

Link to private repository is given by github when you create a new repository.

To push your local repository to remote:

git push -u origin master

It will prompt for login and password of your github account, enter the info and the repository will be pushed. Now if you commit some changes, they will be committed to local repository only. To push the changes to remote, simply enter following command:

git push

To clone a public repository on a local machine, use:

git clone 'link to repository'

To pull changes committed to the project by your project partner to local repo, use:

git pull

That’s it for the basics.

Design a site like this with WordPress.com
Get started