git ugage
What is Git?
Git is a popular version control system.
It is used for:
- Tracking code changes
- Tracking who made changes
- Coding collaboration
Git Getting Started
Git Install
You can download Git for free from the following website: https://www.git-scm.com/
Configure Git
Now let Git know who you are. This is important for version control systems, as each Git commit uses this information:
1 | git config --global user.name "" |
Initialize Git
1 | mkdir myproject |
Now create a file, index.html, and add it in this folder.
Then we check the Git status and see if it is a part of our repo:
1 | git status |
Now Git is aware of the file, but has not added it to our repository!
Files in your Git repository folder can be in one of 2 states:
- Tracked - files that Git knows about and are added to the repository
- Untracked - files that are in your working directory, but not added to the repository
Git Staging Environment
As you are working, you may be adding, editing and removing files. But whenever you hit a milestone or finish a part of the work, you should add the files to a Staging Environment.
For now, we are done working with index.html. So we can add it to the Staging Environment:
1 | git add index.html |
1 | git status |
Now the file has been added to the Staging Environment.
Using --all instead of individual filenames will stage all changes (new, modified, and deleted) files.
The shorthand command for git add --all is git add -A
Git Commit
Adding commits keep track of our progress and changes as we work. Git considers each commit change point or “save point”. It is a point in the project you can go back to if you find a bug, or want to make a change.
1 | git commit -m "First release of Hello World!" |
Git Commit without Stage
Sometimes, when you make small changes, using the staging environment seems like a waste of time. It is possible to commit changes directly, skipping the staging environment. The -a option will automatically stage every changed, already tracked file.
1 | git commit -a -m "Updated index.html with a new line" |
Git Commit Log
To view the history of commits for a repository, you can use the log command:
1 | git log |
Git Branch
In Git, a branch is a new/separate version of the main repository.
Branches allow you to work on different parts of a project without impacting the main branch.
When the work is complete, a branch can be merged with the main project.
New Git Branch
We are working in our local repository, and we do not want to disturb or possibly wreck the main project.
So we create a new branch:
1 | git branch hello-world-images |
Now we created a new branch called “hello-world-images“
Let’s confirm that we have created a new branch:
1 | git branch |
checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:
1 | git checkout hello-world-images |
We are currently on the branch hello-world-images. We added an image to this branch, so let’s list the files in the current directory:
1 | ls |
Now, let’s see what happens when we change branch to master
1 | git checkout master |
The new image is not a part of this branch.
Emergency Branch
Now imagine that we are not yet done with hello-world-images, but we need to fix an error on master.
I don’t want to mess with master directly, and I do not want to mess with hello-world-images, since it is not done yet. So we create a new branch to deal with the emergency:
1 | git checkout -b emergency-fix |
Now we have created a new branch from master, and changed to it. We can safely fix the error without disturbing the other branches.
We have made changes in this file, and we need to get those changes to the master branch.
stage the file, and commit:
1 | git add index.html |
Git Branch Merge
We have the emergency fix ready, and so let’s merge the master and emergency-fix branches.
First, we need to change to the master branch. Now we merge the current branch (master) with emergency-fix:
1 | git checkout master |
As master and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed:
1 | git branch -d emergency-fix |
Merge Conflict
Now we can move over to hello-world-images and keep working. Add another image file (img_hello_git.jpg) and change index.html.
Now, we are done with our work here and can stage and commit for this branch:
1 | git add --all |
We see that index.html has been changed in both branches. Now we are ready to merge hello-world-images into master. But what will happen to the changes we recently made in master?
1 | git checkout master |
The merge failed, as there is conflict between the versions for index.html. Let us check the status:
1 | git status |
This confirms there is a conflict in index.html, but the image files are ready and staged to be committed.
So we need to fix that conflict.
The conflict has been fixed, and we can use commit to conclude the merge:
1 | git commit -m "merged with hello-world-images after fixing conflicts" |
And delete the hello-world-images branch:
1 | git branch -d hello-world-images |
Git Reset
reset is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit.
First thing, we need to find the point we want to return to. To do that, we need to go through the log.
To avoid the very long log list, we are going to use the --oneline option, which gives just one line per commit showing:
1 | git log --oneline |
We want to return to the commit: 9a9add8 (origin/master) Added .gitignore, the last one before we started to mess with things.
We reset our repository back to the specific commit using git reset *commithash* (*commithash* being the first 7 characters of the commit hash we found in the log):
1 | git reset --hard 9a9add8 |
Now let’s check the log again:
1 | git log --oneline |
Git Undo Reset
Even though the commits are no longer showing up in the log, it is not removed from Git.
If you know the commit hash you can reset to it:
1 | git reflog |
Github
Git push into GitHub
First, we create a remote repo in Github. Now we are going to push our local repo to Github.
git remote add origin *URL* specifies that you are adding a remote repository, with the specified URL, as an origin to your local Git repo.
1 | git remote add origin https://github.com/FerryChan666/Project.git |
Now we are going to push our master branch to the origin url, and set it as the default remote branch:
1 | git push origin master |
Now, go back into GitHub and see that the repository has been updated.
Git Pull from GitHub
Any time you start working on a project, you should get the most recent changes to your local copy.
With Git, you can do that with pull.
1 | git pull origin |
GitHub Branch
Create a New Branch on GitHub
On GitHub, access your repository and click the “master” branch button.
There you can create a new Branch. Type in a descriptive name, and click Create branch:
