Let’s begin introduction of Git by gaining an understanding of what it is, and what it can do for you.
Git is software that keeps track of changes that you make to files and directories, and it is especially good in keeping track of text changes that you make. So let’s imagine that you have a document, you have version 1 of that document, you make some changes to it, now you have version 2,
you make some more changes, and now you have version 3.
Well, Git keeps track of those three different versions for you, and it allows you to move
back and forth between different versions, to compare the different versions and to see what changed between each one. Because what it does is manage versions for you, Git is referred to as a Version Control.
To use git you will need to install it first. The first thing you need to know is where to find Git. In the main Git web site, it’s going to be at,
SCM stands for source code manager. When you go to that web site, there will be a link that will allow you to download and then install Git. Before you do that though, I want to also mention another good resource to you, which is that GitHub, one of the popular Git hosting companies, provides very detailed step-by-step instructions with screenshots at this URL.
So if for any reason, things change in the future or if you get stuck, this might be a good resource to help you get unstuck.
Below is the list of git commands that we are going to use over and over again while doing version management.
Staging Index –
Git has the repository and the working copies, but in between is another tree which is the staging index. Git uses three-tree architecture. All the commands we type before commit are related to staging index.
1. Tell Git who you are
git config --global user.name "Sam Smith"
git config --global user.email firstname.lastname@example.org
Configure the author name and email address to be used with your commits.
Note that Git strips some characters (for example trailing periods) from user.name.
2. Create a new local repository
3. status –
It shows what is the status between those three different trees? working directory, staging index and repository
e.g. – Say we are on a master branch and we hit this command for the first time before commit it tells us there is nothing to commit out working directory is clean, that’s letting us know that there’s nothing in the staging directory and the working directory exactly matches what is in the repository and the current branch where the HEAD pointer points to that exactly the same things that we have in our working directory.
Now let’s add some files. Now if we add two new files to the project, and do git status again and see what Git tells us about it. There is a section called untracked file.
It tells that there are files in the directory that are not in staging index.
4. add –
git add file_name
It tells that the files that are not being tracked or modified are in working directory to add them into staging index use git add command followed by the file name.
5. commit –
git commit -m “your message”
what commit does is its going commit the files that are in staging index.
git commit -am “your message”
It adds the files to staging index as well as commit also.
6. diff –
git diff will compare the two, that’s comparing what’s in the repository, the version that HEAD is pointing at, versus what’s in our working directory.
7. diff –staged –
git diff –staged
It’s the option to diff that tells it to look at what’s in the staging index and compare that against the repositor.
8. Delete –
git rm file_name
When we talk about deleting files, we’re talking about deleting them from the repo.
We’re talking about the things that are tracked files.So, the first thing we need to do is add these to our repo, and then we can go about deleting them.
9. rename –
git mv file name changd file name
e.g – if we want to rename the second file as secondary file, let’s git move second_file.txt to secondary_file.txt.
(git mv second_file.txt secondary_file.txt)
10. Undo changes in working directory –
git checkout — file name or directory name
Gets the version that Git has saved for us.
e.g. – We can say, “Git, go back to the repository, get that version, and check it out for me and replace what I have in my working directory with it.”
11. Undo changes to our staging index –
git reset HEAD
If you put something into the staging area that you don’t want there, you can take it out by using git reset HEAD, and the status message will help you remember that.
12. Amending commits –
git commit –amend -m “message”
This command use for undoing changes that made to the repository itself
The git commit –amend command is a convenient way to fix up the most recent commit
13. Retrieving old versions –
git checkout S-H-A — <file name>
(Remember “–” here is bare double dash that we used before with checkout to let it know that we’re not talking about a branch, we mean here is a file.)
step 1 open git log and look at the last commit that we made
step 2 copy the part of S-H-A at the top (these numbers are unique enough that we can usually just grab the first 10 or so characters and copy those and use those for a reference.)
step 3 Put dash dash (–) after S-H-A and then type file name after dash dash
E.g. Let’s take a look at git log, and let’s take a look at last commit that we made. Now we could use amend to just edit that commit. But there is another way that we can do this. Lets make some change in file. Lets say we made two or three commit since then and we wanted to go back and we wanted to rework this file and get it back to old state.
What we can do is we can checkout this file, check out the old version of that file from before we made that change. So if the change was made in last commit then the commit that has the latest version previous to that is the old version.
So now we type git check out and copy the part of S-H-A of that old commit dash dash and then the name of that file
14. Retrieving commits –
git revert S-H-A
When we want to undo the changes for a commit completely and totally, we can use the Revert command.
step1 open git log
step2 grab a part of the S-H-A reference to old commit that we want back.
git reset –soft/mixed/hard S-H-A
This is very powerful tool in git that allows us to undo multiple commits.
What git reset does is it allows us to specify where the HEAD pointer should point to.
There are three types of soft, mixed & hard.
Soft is going to move the HEAD pointer to the specified commit, and it’s not going to change the staging index, or the working directory at the same time. It’s just going to move the pointer.
What mixed reset does is it moves the HEAD pointer to the specified commit, and it also changes the staging index to match the repository. It does not change your working directory though. So, at this point, the staging index and the repository will be set in one place, our working directory, though, has all those changes that we’ve made All the things that were in later versions of the repository are still in our working directory. We haven’t lost any work.
A hard reset will not only move the pointer of the repository, but it will make your staging index and your working directory match that as well. So that means any changes that came after that commit are completely obliterate. They don’t exist in the repository, the staging index, or the working directory.
16. Removing untracked files
git clean -n or -f
When we have a lot of files that have been added to our working directory that are not tracked that we don’t want, we just want to get rid of then we use this way.
Git gives us a quick and easy way to just tell all those files that should be thrown away. So specially if you have things like log files, zipped directories, compiled code, things like that that you don’t want in your Repo, and you’d like to get them out of your working directory too. We can get rid all of those by using the git clean command. -n options tell us the files it would remove. Don’t actually remove anything, just show what would be done. -f forces it to run.
17. Navigating the commit – Tree-Exploring tree listg
A tree is a directory. To list out a tree, we use git ls-tree, listing the tree, and then what we pass in after that is a tree-ish.
18. Show commit
git show S-H-A
Shows full commit with date and author
19. Compare commit
git diff S-H-A
20. list of branches
The way we can see all the branches in our local repository is git branch
21. create branch
git branch <branch name>
22. Reference to existing branches
When we add new branches this is where they show up, it adds a new reference in the HEADs folder
23. Checkout the branch
git checkout <branch name>
This command tells us that it switched to the branch.
24. Create and checkout branch at same time
git checkout -b <branch name>
This command basically tells check this out as a new branch, it’s little bit counterintuitive
25. Compare branches
git diff <currently active branch name>..<other branch name>
26. Renaming Branch
git branch -m <branch name to be change> <new branch name>
27. Deleting Branch
git branch -d <branch name>
28. Merge Branches
git merge <branch name>
The first step is that we want to make sure that we checkout the branch that things are being merged into, the receiver. And then we merge our changes from that branch to the branch in which we want the changes to be merged.
29. Resolving Merge Cs
git merge –abort
Three choices as to what to do to resolve these conflicts.
First, we can simply abort merge. Second is resolve conflicts manually.
30. Adding remote repository
git remote add <alias> <url>
31. git remote and git remote -v
Git remote shows the name of our alias . It shows the URL that it’s going to use for fetching and the one that it’s going to use for pushing.
32. Remove remote repository
git remote rm <alias>
33. push code to remote branch
git push -u <alias> <branch>
e.g. git push -u origin master
***If we don’t do git push with the -u option, it does not track any remote branch. All it does is push our code up there, and that’s it. The -u option says push it up there, and also make a note of the fact because we’re going to be coming back and working with this branch frequently.
34. Show remote branches
git branch -r
35. Show remote and local branches
git branch -a
36. Take copy on the remote to local (clone)
git clone <path from github/bitbucket/gitlab account>
e.g. git clone email@example.com:Virajb/3dcadvisuals_wordpress.git
If the projects already exist then it says that it is already exists, But thats no big deal. We can call the folder name that exists in, anything we want. The name of the directory has nothing to do with the name of the repository..
e.g. git clone firstname.lastname@example.org:Virajb/3dcadvisuals_wordpress.git <your folder name>
Tracking is, we could have one local branch that stays rather closely in sync with the progress of another branch.
When we do git clone it does track the remote branch
37. Tracking remote branches
e.g If we create a new branch and do just a git push without using -u option then it will just push the code to remote without tracking the branch. It we want to make it tracking then we can use the git config commands.
git config <branch name>.remote <name of the remote>
e.g git config branch.non_tracking.remote origin
git branch –set-upstream (which is also what the u option is, that -u that was used, when we did a push) <branch name> <path>
e.g. git branch –set-upstream testBranch origin/testBranch
This will tell it what its up-stream server is. It will tell it what the remote is that it ought to track.
38. Pushing changes to remote repository
If the branch is tracking branch then we can just type git push.
39. Fetching changes from a remote repository
Fetch is what synchronizes origin/master with whatever is on the remote repository.
40. Merging in fetched changes
git merge origin/master
when we do a merge, we are merging with origin/master. Our local version, that’s where we are merging in.
when we do a merge, we always want to make sure we do a fetch first.
41. git pull
git pull is equal to fetch + merge. It does it all in one step.
42. Checkout remote branches
First create branch on the remote git branch <branch name> <origin/branch name>
e.g. git branch testBranch origin/testBranch
git checkout -b <branch name> <origin/branch name>
e.g. git checkout -b testBranch origin/testBranch
43. Pushing to an updated remote branch
***Just keep that in mind, if you cant push the remote server, you’ll just need to fetch, then merge, then push again.