How to Use Git and GitHub
Git is a very famous version control system popular amongst software developers. Have you ever worked on scripts only to wish you could go back to the code you had yesterday or the day before? With Git, you can return to any point of the project’s history you want.
You can get Git from . Once you have it on your machine, you may start a new Git project by opening Command Prompt/Terminal, navigating to the folder where your project is (with the help of the cd command) and typing gitinit.
You should get a message similar to:
Initialized empty Git repository in C:/…
Now, you can work on the files in that directory and whenever you feel the changes are sufficient to be saved so you can go back to them you can type: git add –all. This will add all untracked (new) and changed files to the staging area. The final step is to write git commit -m “Message to describe the changes made”with a descriptive message and your changes will be saved. You can repeat this process as many times as you want throughout your project.When you execute the commit command you will get an output similar to:
[master (root–commit) c3ea8d2] Hello
1 file changed, 1 insertion(+)
create mode 100644 d.txt
If you typegit log you will get the message, time, author and a commit id of all the changes in the project’s history.
git log will return something similar to:
Date: Mon Nov 23 11:24:25 2015 +0200
Date: Mon Nov 23 11:21:13 2015 +0200
The cool thing about that is that you can use the commit id to return to any saved point (2 such points shown above). When you switch between commits, all the files in your project’s folder will match what was present in that 翻墙 commit. For example, if your latest commit created a file called app.js and you switch back to the previous commit – app.js will disappear but if you switch again to the latest commit it will reappear in your project’s folder.
Here is how you can do this:
You type git checkoutcommitId, consistent with the above examples we typed git checkout c3ea8d2c5f4a782572a120b3f82e31aa633b7359.
Now, if you want to return to the latest commit you can type git checkout master where master can be substituted by the branch you are working on.
Branches are just that, like branches of a tree, they should be used for different parts and features of the application. Two people can be working on different things in different branches and when they are finished, they would attempt to merge the changes into a single branch for the ready product. If one person saves changes or messes something up in his branch, the master branch or the other people’s branches would be alive and kicking.
You can see what branches currently exist for a project by typing git branch.
Here is an example of our current branches:
The star sign serves to indicate to us that we are currently on that branch, in our case – the master branch.
You can create a new branch by typing git branch branchNameand you can switch to that branch by typing git checkout branchName.
If we now type git checkout helloWorld, we will get a message similar to:
Switched to branch ‘helloWorld’
To see if we have made changes to the files inside our project and did not save them we can type git status. Typing git status when there are no new changes would print to the command-line something like:
On branch helloWorld
nothing to commit, working directory clean
Otherwise, the new and the changed files and folders will be displayed.
In case you want to save your project online you can use the git push command. As your project started on a local machine you have to specify the URL to push to. gitpush https://github.com/userName/test.git masterwould push the master branch of our local project to the test repository of userName in GitHub. Alternatively, you can push any other local branch by changing master to the branch’s name. You will be prompted for credentials when pushing to GitHub.
If you want to work on an already started project, you can clone it (which will set up a local repository in the folder where you are located in your Terminal when typing the command) using git clone. You just type git clone projectURL.
Figure 1: Getting the URL necessary to clone a repository in GitHub
If you have cloned a repository, you can use git push without specifying a URL.
Leave a Reply