How GIT is different from other source control system?
- Git creates a snapshot of the working directory in the local repository for every commit unlike maintainig versions of the changed files in other source control systems like CVS.
- Users can create a copy of the remote repository into their local machines and work without the need of network connection
- In the local repository, users can perform all the operations as they can in remote. Changes can be pushed to the remote repository only when they have to be shared across the team.
What are the different stages in git?
- Working directory -> Staging -> Local repository -> Remote Repository
- Except Remote repository, everything lies in a local machine.
- Working directory is where a developer makes code changes. Your IDE like Eclipse, Intellij Idea
- git add -> moves the working directory changes to the staging directory.
- git commit -m -> moves the staging directory changes to the local repository.
What is master?
- It is the default branch in the local repository. Its HEAD points to latest commit.
What is HEAD?
- Git stores all the commits of a branch as a chain linking them together with HEAD always pointing to the latest commit of the current branch.
How to fetch the code from the remote branch?
- git clone -- Creates a local repository by taking the snapshot of the remote repository at a current point in time.
- git fetch -- updates the existing local repository with the latest changes from the remote repository. You can run this only if you are having a local repository. This is the main difference b/w clone and fetch
- git pull - fetch and merge into local working directory.
How to make code changes?
- git branch <branch name> - creates a branch in the local repository. Basically, creates a snapshot of master repository and tag it with a branch name. This is where developer makes code changes for a fix or a new feature.
- git checkout <branch name> - to switch between branches. Easy way to work on multiple fixes/features at a time. while switching, git updates the working directory with the branch content.
- git commit - moves the changes to the local repository. Used for saving the changes in the local repository.
How to merge the changes from a feature/developer branch to master branch in local?
- git merge <feature branch> - This command will merge the changes to your current branch. If you have executed git checkout master prior to executing this command, it will make the current branch active and merge the feature branch changes to the master.
- The output of the merge is a commit with the HEAD pointer of the branch moves to the latest commit.
- By default, git tries to merge using fast forward merge. If the merge is not trivial, 3 way merge will be used.
- fastforward merge - If a new commit does not have any intermideate commit b/w itself and master. Git fast forwards the master branch pointer to the new commit as a short cut. Its an optimization that git does.
- 3 Way merge - If there are any intermediate commits in master after you branched out feature branch from the master. Then the merge will happen b/w 3 sources. The sources that are involved in 3 way merge are Common ancestor commit on the master branch which master and feature have in common, feature branch latest commit and the master branch latest commit. The output of the 3 way merge is a new commit.
What if I have a conflict while merging?
- While doing a 3 way merge commit, if git encounters any merge conflict, it prompts you to resolve the merge conflicts first.
- You can run git mergetool to visualize differences, make changes and finalize your merge by accepting git's question while exiting the tool.
- After the merge was successful, issue a git commit to finalize the merge commit.
What is rebase? Why rebase?
- rebase - Special merge. It merges the full branch history instead of creating a new commit
How to push the local branch changes to the remote branch?
- git push origin <branch> - for pushing the local repository changes to the remote
- Origin - alias for the remote reference or cloned repository
- All remote commands has the syntax for example, git push origin master.
- It can be read as git remote command followed by default remote repository reference follwed by default local repository reference or you can call simply git push
How to work on multiple features/tasks?
- git stash - to take backup of the working directory and make the working dir back to the state where it was last committed. Used for saving the uncomitted work.
- Useful for switching b/w different tasks
- git stash for creating a stash
- git apply stash <stash id> for applying the stash
What is Fork?
- Fork - copying the repository from someone namespace to your namespace in the github.
More commands:
- Commands used for comparing the files b/w stages.
- git diff - compares working directory and staging directory
- git diff --staged - compares staging directory and local repo
- git diff HEAD - compares the working directory and local repo
What is .gitignore?
- If we want certain files in our working directory not to be tracked.
Comments
Post a Comment