Today, everybody is using Git to manage project version control job. Here I don’t want to mention how hot the Git is. But I want to say that if you don’t know how to use Git, you will be out of the IT industry. As an old IT developer, I has been working on SVN for several years. To be honest, it is tough to move from SVN to Git, though Git is a little bit similar with SVN. However, we must follow the trend. The best way to learn how to use Git is starting a new project and using Git to manage the version control in command line. In this post, I will record how I start my first project with Git. I think it will be very helpful for another people who is new to Git. As a beginner, I may make some mistakes here. So please leave a reply if you find any mistakes.
Start Git with a New Project
Let’s create a new project and use Git to manage version control. For instance, I will create a new folder “helloworld” and I will put all my project files in this folder. In the command line, I will go to this folder and initialize a Git repository with following commands:
cd helloworld git init
Add Files to Git Repository
After we successfully initialize the git repository in the project folder helloworld, we can start to add files into the project folder. I will create a new file “index.html” and then I will put this file under version control with following commands:
git add git commit
Change Files and Commit Changes to Git Repository
Different from SVN, Git will require you to add “local” changes to the “index” (this is called “stage files”) before you can commit the changes to Git repository. For example, we already add “index.html” in Git repository in above step. Then, we make some changes. If we want to commit the changes, we need to run following command:
git add git commit
Because the file “index.html” is already under version control, we can also use one line command to commit the changes. But please keep in mind, following command actually does two jobs: stage files and commit changes. This command will not affect new files which are not under version control. You can check the git official document about git commit.
git commit -a
However, git add doesn’t handle deleted file. To stage deleted file, we can run following command before committing changes to git repository.
git rm file.txt
Above command will stage the file for deletion. If the file is not deleted in hard disk yet, this command will delete it from hard disk. Of course, the file can be restored since it was previously committed in repository.
After running this code, please don’t forget to commit the changes as it is still in the stage.
Before staging files to commit, we may want to check which files are changed locally, we can use this command to check changes between local working copy with index copy.
However, there is a very complicated logic about “change”. In human words, the file can be in 3 status:
- Working Copy: files in local folder (may be changed, but not ready to be committed yet
- Index Copy: files in local which have been changed and staged to commit to repository
- Head Copy: files in the Git repository
So git provides 3 commands to check changes between these status:
- Working Copy against Index Copy: git diff
- Working Copy against Head Copy: git diff HEAD
- Index Copy against Head Copy: git diff –cached
Questions and Answers (Q&A) for Git
Where is Git Repository?
If you were working with SVN or CVS in the past, you may have the same question. As Git is a distributed version control system, it doesn’t need a central server to store its repositories. It is the biggest difference between Git and SVN. If you initialize a Git repository with command “git init“, the repository will be located in .git directory under current path. For more information, you can check the git official document about git init.
How to know which files are ready to commit?
Sometimes we may make lots of changes in working copy and forget which files are working copy and which files are index copy. In this case, we can using following command line to see the files status:
After running above command, git will give us a brief files status of all changed files. The status includes:
- Changes not staged for commit
- Untracked files
Basing on these information, we can decide what to do in next step.