Working Locally with Git
Hi, I am Malathi Boggavarapu and welcome to my blog.
This tutorial describes how to create a local Git repository, adding and committing files to local repository, viewing the history of the commits, knowing the file changes made between two commits and also we discuss about Git ignore and so forth. I have a seperate post "Git Fundamentals" in my blog which explains everything about Git but i had broken them down to smaller sections to make it easy for everyone to follow.
So let's get started.
Let me explain the commands in the below image so that it would be easy to understand.
git init - It creates .git directory which contains a repository and all of its metadata. I can add file to this repository by echoing "Hello, Git" to a README.txt file.
echo "Hello, Git" > README.txt
git status - Tells me that README.txt is an untracked file and git does not have it in repo yet.
Try adding the file to staging area using git add README.txt
Now again try git status. Git now notices that the file is a new file and is staged to be added to a repository which i can do by running git commit
git commit brings up the default text editor where you can add commit message.
git log - You can see the commits that are done in the repo. You can see Author, date information and also the commit sha. Git identifies the commits by the sha1 hash of the commit.
update the README.txt file by adding a second line "Hello, again"
git add -u - You can add all modified files to the repo.
git commit -m "Updated README.txt" - Commit a file with commit message inline.
If i want to find what is changed between these two commits, i can do as follows
git diff dd6819..a15ec6 - Note that the "Hello, again" has been added (in the above picture)
So always working with sha1 hashes can be difficult so Git provides easy way of specifying these things. So we can also do it in easy way as below
git diff HEAD~1..HEAD
The latest commit is known as HEAD, i can also go back from HEAD by using ~1. The above command shows the differences between the latest commit and first commit as per our example
We can also specify it in more easy way using git diff HEAD~..
Let's add few more files now using touch command.
touch file1.txt file2.txt
git add -u - Going to add all updated files to staging area. Git has a notion of this staging area which of the files or changes that are going to add in next commit. Remember that -u option only adds the updated files to the staging area, that means files that has been updated or deleted.
git add -A - This A option adds all files including untracked ones. You should be careful while using this option to make sure that you are not accidentally adding files that you are not intended to.
In our example, it will add file1.txt and file2.txt files to the staging area.
You can also delete and rename files and follow the same sequence of commands such as add and commit to add to the staging area and commit the files.
git reset --hard - Reset the working copy back to the HEAD.
git clean -n - It shows the files that would be removed.
git clean -f - Removes the temporary files from the repo.
It's easy way to clean up the working copy.
/logs/*.txt
/logs/*.log
/logs
you can add any format but make sure the paths are relative to your repository.
So we came to the end of the session. Hope it is helpful. Please post your comments and let me know your questions and valuable inputs.
Follow my other posts on Git which were mentioned below
Brief History of Version Control Systems, DVCS Advantages and Git Installation
Configuring Git in your computer
Git - Creating, Verifying tags and Pushing to Remote repository
Git - Branching, Merging, Rebasing and Cherry-picking
Happy Learning!!!
This tutorial describes how to create a local Git repository, adding and committing files to local repository, viewing the history of the commits, knowing the file changes made between two commits and also we discuss about Git ignore and so forth. I have a seperate post "Git Fundamentals" in my blog which explains everything about Git but i had broken them down to smaller sections to make it easy for everyone to follow.
So let's get started.
Working Locally with Git
Creating a local repository, adding files and committing changes
I have an empty directory GitFundamentals which i would like to change to local git repository.Let me explain the commands in the below image so that it would be easy to understand.
git init - It creates .git directory which contains a repository and all of its metadata. I can add file to this repository by echoing "Hello, Git" to a README.txt file.
echo "Hello, Git" > README.txt
git status - Tells me that README.txt is an untracked file and git does not have it in repo yet.
Try adding the file to staging area using git add README.txt
Now again try git status. Git now notices that the file is a new file and is staged to be added to a repository which i can do by running git commit
git commit brings up the default text editor where you can add commit message.
git log - You can see the commits that are done in the repo. You can see Author, date information and also the commit sha. Git identifies the commits by the sha1 hash of the commit.
update the README.txt file by adding a second line "Hello, again"
git add -u - You can add all modified files to the repo.
git commit -m "Updated README.txt" - Commit a file with commit message inline.
Viewing history and diffs
git log - It shows the two commits in Git repo which are listed in reverse chronological order. So the more recent commits are at the top and latest commits are at the bottom. If you notice the below picture, it shows commit hashes too.If i want to find what is changed between these two commits, i can do as follows
git diff dd6819..a15ec6 - Note that the "Hello, again" has been added (in the above picture)
So always working with sha1 hashes can be difficult so Git provides easy way of specifying these things. So we can also do it in easy way as below
git diff HEAD~1..HEAD
The latest commit is known as HEAD, i can also go back from HEAD by using ~1. The above command shows the differences between the latest commit and first commit as per our example
We can also specify it in more easy way using git diff HEAD~..
Let's add few more files now using touch command.
touch file1.txt file2.txt
git add -u - Going to add all updated files to staging area. Git has a notion of this staging area which of the files or changes that are going to add in next commit. Remember that -u option only adds the updated files to the staging area, that means files that has been updated or deleted.
git add -A - This A option adds all files including untracked ones. You should be careful while using this option to make sure that you are not accidentally adding files that you are not intended to.
In our example, it will add file1.txt and file2.txt files to the staging area.
You can also delete and rename files and follow the same sequence of commands such as add and commit to add to the staging area and commit the files.
Undoing changes to the working copy
If i want to undo the changes of a file, simply do git checkout [filename] to pull that out of the repo. By default it grabs the HEAD version. You can checkout files form the repo inorder to cleanup or revert changes that you made by mistake or changes that are a bad idea.git reset --hard - Reset the working copy back to the HEAD.
git clean -n - It shows the files that would be removed.
git clean -f - Removes the temporary files from the repo.
It's easy way to clean up the working copy.
Ignoring files with .gitignore
Now let's say i do have something like log directory which contain some log files. Logs are constantly changing as the application runs and i dont want to commit these log files to my repo. So git provides .gitignore file. Open the file and add info to it./logs/*.txt
/logs/*.log
/logs
you can add any format but make sure the paths are relative to your repository.
So we came to the end of the session. Hope it is helpful. Please post your comments and let me know your questions and valuable inputs.
Follow my other posts on Git which were mentioned below
Brief History of Version Control Systems, DVCS Advantages and Git Installation
Configuring Git in your computer
How to Git clone your remote repository
Git Fetching, Pulling from Remote repository and Pushing to Remote repositoryGit - Creating, Verifying tags and Pushing to Remote repository
Git - Branching, Merging, Rebasing and Cherry-picking
Happy Learning!!!
Comments
Post a Comment