Git

Git

Raghav Gupta

Git is a decentralized and distributed version control system, developed by Linus Torvalds. Most people still choose to use it with a central server to serve as the main repository for a project or team. Because of distributed nature, Git can scale massively.


Repository

Repository contains files, history, config managed by Git.

Git Repository.


Four states of Git

  1. Working directory
  2. Staging Area (Git Index) - pre-commit holding area. It is a holding area for queuing up changes for next commit. You can move files in-and-out without touching repository and it's history of changes.
  3. Commit - Git Repository (History). It is a local repository.
  4. Remote State (GitHub). It has it's own three stages internally.
Git status flow.


Branch

Conceptually, branches work like they do in other source control systems: they are timelines that contain your changes. In Git, branches contain commits. When we start off, Git provides us with a default branch named master.

  • If we want to have a new branch:
git branch branch-name
  • Switch to another branch:
git checkout branch-name


Set-up:

  • Check Version:
git version
  • Set username and email:
git config --global user.name "Raghav Gupta"
git config --global user.email "18raghavgupta@gmail.com"

let's conform:

git config --global --list
  • Now make a repository on GitHub and clone it in your system:
git clone https://github.com/raghav18gupta/python-advance.git
  • Check the status:
git status

The git clone command automatically set up a relationship back to the repository on GitHub


First Commit

Now create a file "first.txt" and add some lines. Git status says that we have an untracked file. An untracked file is just a file in our working directory that hasn't been added to Git yet.

git add first.txt

Now if we check a "git status", Git tells us there is a new file in the staging area, which Git describes as changes to be committed.

So, now that our file is in the staging area awaiting the commit, we haven't committed anything yet.

We can still back out the change from the staging area, and not impact Git's history in any way.

The staging area is designed to allow you to build up several related changes, so they can be committed as a single atomic unit.

git commit -m "commit-message"

 Now if I do a "git status", Git tells us that we're back in a clean working directory state, and that our master branch is ahead of origin master by one commit.

So, now the new file has been moved from the staging area, into the local repository.

Since there are no other pending changes, once the commit happens Git marks the working directory as clean.

git reset first.txt

This command removes file from staging area. If all file at once need to be removed:

git reset


First Push

git push origin master

"origin" is a remote branch name, which was set up for us automatically when we cloned the repository from GitHub, and the name of the branch to push, which is "master", since it is the only branch we have.


Git ignore:

.gitignore file helps in listing out items you want to ignore from committing.

*.pyc
.DS_Store


Remote branch:

  • Viewing information about remote branch:
git remote -v
  • List all local and remote branch:
git branch -a


Diff:

Add/remove some lines in file and this command shows those changes.

git diff


Pull:

It will pull any changes that made since the last time that we pulled from that repository.

git pull origin master


Merge branches

  • First checkout to master branch:
git checkout master
  • Now in case anybody else updated the master branch, it is good to pull before commit to master.
git pull origin master
  • To check the branches that we've merged in so far:
git branch --merged
  • Finally, to merge a branch to a master:
git merge branch-name
  • And at the end, you can push the changes to remote branch:
git push origin master


Deleting a branch

  • To delete a branch locally:
git branch -d branch-name
  • To delete a branch from a remote repository:
git push origin --delete branch-name


Report Page