Skip to Content
Technical Articles

Basic introduction to Git

Git is a Distributed Version Control Systems. here is a brief introduction to it.

1.Common Git commands

init

The “git init” command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository.

clone

The “git clone” command copies an existing Git repository, e.g. from remote location github.com. Cloning automatically creates a remote connection called origin pointing back to the original repository.

add

The “git add” command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.

commit

The “git commit” command commits the staged snapshot to the project history. Snapshots are committed to the local repository, and this requires absolutely no interaction with other Git repositories.

push

The “git push” command is to transfer commits from your local repository to a remote repo.

branch

A branch represents an independent line of development. New commits are recorded in the history for the current branch, which results in a fork in the history of the project. The “git branch” command lets you create, list, rename, and delete branches.

fetch

The “git fetch” command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with. This gives you a chance to review changes before integrating them into your copy of the project.

checkout

Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

pull

The “git pull” command is to merge upstream changes into your local repository. you can also do this with “git fetch” followed by “git merge”.

merge

The “git merge” command is to merge all committed changes on a specified branch into current branch.

rebase

Rebasing is the process of moving a branch to a new base commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base, it’s literally rewriting your project history. The commits that were previously saved into the temporary area are then reapplied to the current branch, one by one, in order.

Assume the following history exists and the current branch is “topic”:

          A---B---C topic
         /
    D---E---F---G master

Now executes the following command.

git rebase master

The branches will be:

                  A'--B'--C' topic
                 /
    D---E---F---G master

It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolve any such merge failure and run git rebase --continue. Another option is to bypass the commit that caused the merge failure with git rebase --skip. To check out the original <branch> and remove the .git/rebase-apply working files, use the command git rebase --abort instead.

2.Steps for common tasks

Commit changes to remote master directly

  • Make some changes to file01.txt in your working directory.
  • git add file01.txt
  • git commit -m “Commit M1”
  • git push -u origin master

If any conflicts:

  • git fetch origin
  • git merge origin/master
  • change the conflict file to solve conflicts
  • git add file01.txt
  • git commit -am “fixed conflicts”
  • git push -u origin master

Create a branch and commit changes to it

  • Open a new branch named feature01: git checkout -b feature01
  • Make some changes to file01.txt
  • git add file01.txt
  • git commit -m “Commit feature01”
  • git push -u origin feature01

Merge a branch into master branch

  • Suppose you have committed all changes to branch feature01
  • git checkout master
  • git merge feature01
  • git push -u origin master

Delete a branch

  • Delete a local branch: git branch -d feature01
  • Delete a remote branch: git push origin –delete feature01

Rebase a branch on last commit on master

  • git checkout feature01
  • git rebase master

Merge a branch by rebasing it on master

  • git checkout feature01
  • git rebase master
  • git checkout master
  • git merge feature01
  • git push -u origin master

Get latest changes on a remote branch

  • git checkout feature01
  • git pull origin
Be the first to leave a comment
You must be Logged on to comment or reply to a post.