Git Overview
Git is a distributed source control management system. Git is a relatively fast source control program and has built-in data integrity check. Using git, it is easy to create an experimental branch and then either bring merge it back to the main branch or discard the experimental branch.
Getting started
-
Get the Git scm tools from the https://git-scm.com/downloads. MacOS already comes with it. You can check if your computer already has Git by running this command in a console.
You can run the Git commands in the regular OS console (Terminal in MacOS and Command Prompt on Windows) or through Git Bash.
git --version
-
Setup your username and email address by running the following commands:
git config --global user.name "John Doe" git config --global user.email "john@doe.com"
-
You can confirm the setting took effect by running the following commands. These are the same commands as above for setting the username and email address, but without providing values in quotation:
git config --global user.name git config --global user.email
Git will tag this username and email address as the author for the source code change commit. This is a global setting, and it is possible to override global setting on per repository level.
Initializing a new git repository
Initializing a new git repository to an existing project or to an empty folder, it is the same command. Go to the root of the project's directory and run the following command
-
git init
-
The git status command will show the basic information about the repository, such as which branch repository is currently on, commits, any tracked files that was stagged to be committed, and any untracked files.
Once git initialized done on the directory, git will automatically monitor all the files from the that directory and subdirectories.
git status
This is an example of a git status output on a git initialized project directory that is empty.
This is the git status output on a repository with files. It shows that the existing files are untracked.
Adding files to git for tracking
-
We can add file to git for tracking. Run the command git add [filename].
git add index.html
-
Since this is a new git repository to an existing project with files, we can also add all existing files to be tracked. Run the command git add .
git add .
Committing the changes
Once the files has been stagged for tracking, we can commit the changes to git.
-
Use the command git commit --message "the summary of change message here" to commit the changes that are done to files being tracked
git commit --message "Initial commit"
Alternatively, we can also use git commit -m "initial commit". This has the same effect as git commit --message. This is just with a shorten parameter name for --message.
-
Perform the git status command to double check if we are working off clean tree.
-
If you try git commit on a repository where there are no files are being tracked or no changes were made to files that are being tracked, you would get the following message
We can use the command git add . to have all the files and sub directories tracked by git. Or use git add [filename] to add files individually.
Git log
-
To see the list of recent commits to the branch you are in, type in git log to see the last 5 commits.
git log
-
If the number of lines for the output of the git log does not fit the command prompt or terminal you are using, the output will be displayed through a less program. This lets you scroll up and down, and once you reach the end, it will show like this:
Note the '(END)' in the last line of the screenshot.
To exit this less program, press q to quit that program.
-
If you want to more or less than the last 5 commits, you can use the - n (or --max-count) parameter to indicate the number of last commit you want to see. So, type in git log -n [# number].
-
See git log for more details
Git Ignore
Usually, a project has files that are only used for local development and these files that should not be committed to source code to be tracked. Git provides a way to ignore tracking these files. Create a file called .gitignore at the root of the source repository directory. List files and directory paths that should not be tracked.
For example, in a node.js project, we have node_modules directory that are only for local development and threfore should not be committed to a remote repository.
In this case, add /node_modules in the .gitignore file.
We can also add wild card pattern to gitignore. For example, if we want to ignore all files with .log extension, add *.log in the .gitignore file.
/node_modules
*.log
Published on Last updated on