GIT concepts and cheetsheet

Git does not take care of the access control so we need to additional software for that and that’s where hosted solutions like GitHub and BitBucket come in and also self-managed solutions like Gitosis and Gitorious come in.

Help

Config

 

Starting a Repository

Never need to go in the directory but that’s where the data is stored.

Git Workflow

Creating a file – It will be untracked initially

Add file to staging area – Is getting ready for taking the snapshot of the file.

Commit – Taking a snapshot of the files that we have put on stage

This takes the snapshot of the stage and this gets added to our timeline

Keep commit messages in present tense and not the past tense

Different ways to add files

Git Diff

Red color shows the removed line and green color shows the line that was added.

Unstaging files

HEAD – refers to the last commit in the current branch

Discard modifications

Skip staging and Commit

However this will not add the untracked files.

Undoing a commit

 

Should not be used after the push

Adding to a commit

This new commit message overrides the previous commit messages

Sharing the repository

Adding a Remote

Remotes are like bookmarks

Pushing to Remote

We could password cashing to avoid typing the user name and password again and again.

https://help.github.com/articles/set-up-git

github.com has history and commit options which provide the info similar to git log

Remove a remote

Cloning and Branching

Cloning a Repository

Steps

  1. Downloads the entire repository to a new gir repository
  2. Adds the remote pointing it to the clone url
  3. Checks out the initial branch likely master and set the head

Branching Out

Working on a branch

Now when add this file and commit it then this file is added to our current branch and not the master branch.

Merge a Branch

Move to the branch you want to merge it with.

then

* Fast forward merge means that when we merge a branch there is no file merge required as the master branch did not change in the time while we made the changes.

Delete a branch

NON Fast Forward Merge

Some commit done on both master and feature branch

Now when we move back to master from the feature branch and try to merge we are taken into the Vi editor and it asks us to do a merge commit.

Vi Commands

We can modify the message

When git does a merge commit then there a commit a commit at the top. If we see the git log it says that there has been a merge commit. At this point the 2 branches became one branch

Collaboration

Someone already pushed a new version of source code after you pulled but before you push. So now if you try to push your code it will be reject as you are behind the commit at this point of time.

Understanding Pull

  1. Fetch (or sync) our local repository with the remote one –  git fetch
  2. Merges the origin/master(repository on the server) with master –  git merge origin/master

So this has merged the local master branch with the code from branch but the origin/master is not aware of this merge commit

So we need to do a Push

Updates origin/master be at the same state as our local repository

Merge commits are not considered to be good as they pollute the repository
We have 2 people editing the same file and one of them has committed the file back to github and other person now wants to commit the same file.

Pull will sync the repository and then tries to merge but the merge will not be successful. So we need to edit the file and do a merge commit.

When we jump into the file we will see something like a diff of the local version and the server version

So we need to manually edit the file to fix it.

The

Then

Remote Branches

  • When you need other people to work on your branch
  • You might want to back up a branch if it’s going to there for long

We make some changes on the branch and when we do a commit and a push the also pushes the changes from the local branch to the remote branch

We can see the list of all the branches in github

Now when someone else does a pull from remote then it will show that a new branch is be added to the remote.

But if check the local branches it will still not be available

but we see the remote branches we will be able to see the new remote branch

so we could do the

So now you could make the changes and push it.

Shows

  • All the remote branches whether they are tracked or not.
  • Shows all the local branches and what remote branches they merge with.
  • Local branches configured for git push. Also shows the local branches that are out of date.

Removing a Branch

If there are commits on this branch that have not been merged it will ask to either commit those changes

Push on Deleted Remote Branch

Prune

We would have 2 copies of a remote branch locally. One is brach that we work with and the other one is a read-only copy of the remote branch. We could check that by running

Once we are done working with a brach we delete the branch but we might also want to remove the read-only copy of the branch maintained by git as well. We could do that by using the Prune command which removes all unreachable objects from the database.

We could also use gc which automatically calls prune

Tags

A tag is a reference to commit. It is mostly used for the release versioning.

Rebase

Merge commits are not so great so we want to skip them. So Someone already pushed a new version of source code after you pulled but before you push. So now if you try to push your code it will be reject as you are behind the commit at this point of time.
So instead of git pull and git push we could do git fetch and git rebase

Rebase does the following steps.

  • Moves the changes form master which are not in origin/master to a temporary area.
  • Then it runs all the origin master commits one by one
  • Then it runs all the commits in the temporary area onto the master one by one.

So the merge happens without any merge commit.

Local Branch Rebase

We have 2 people editing the same file and one of them has committed the file back to github and other person now wants to commit the same file.

So need to fix the merge conflict and then run git rebase – – continue

once we are done with the editing of the file we do a

History

It shows

  • SHA
  • Author
  • Date Commit message

%ad – author date

%an – author name

%h – SHA hash

%s – subject

%d – ref names

* Run git help log for more options

Aliases for log output formats

To use this log formatting just use

Shows the graphical output

Date and Time Ranges for Log

Uncommited Changes

Comparing with earlier commits

We could also use abbreviated sha’s available in github

* Can use time based diff just like logs

Blame

Excluding files and folder

To exclude a folder we need to put the folder name inside the .git/info/exclude file and then we will not that folder anymore.

So we can exclude

Specific file –  tutorial.mp4

Specific file type –  *.mp4

Specific directory –  experiments/

.log files in log directory –  logs/*.log

Excluding From All Copies

.getignore –  To ignore the file not only from the local repository but also from the all the repositories

Removing a File from the repository

and then we commit

Untracking File

Aliases

 

Data types in git

blob, tree commit, tag

Git takes snapshot and not the diff. So it takes the snapshot of the database at any point in time.

Difference Since some date

 

Reset author on previous commit

Reset author on all commits

Any questions, comments and feedback are most welcome.