arrow_backBACK My git notes

 BASICS

A: Starting a new project

mkdir my-new-working-directory
cd my-new-working-directory
git init

B: Cloning an existing project

Navigate to directory, where you want the cloned files to be copied. Remember that git clone will automatically create a new working directory named after the repository on the server by default.

git clone https://address.of.repo/git-project-to-copy.git

 

Now that we have a project started, typical workflow would be like this:

Modify files at your local working directories.

Adding (changed) files to Git

git add --all

This added modified files to so-called staging area (also called index).

Check the status

This is OPTIONAL, but it is nice to see what are the files to be committed.

git status

NOTE ABOUT RESET: If you realize now after checking staging area status that this was not what you wanted to commit after all, don't worry, you can reset the staging area with git reset command:

git reset

Commit changes to local repository

git commit -m "Your comments about this commit."

NOTE ABOUT RESET: If you want to undo the last commit but preserve the changes done to the files, use:
git reset --soft HEAD~1
With --hard option you will lose all changes done to files, so be careful.

Push changes to GitHub

git push origin master

Nowadays the default master branch is named main.

 

 BRANCHES

View all branches

git branch

* master

Here there's only one branch - master - and the asterisk tells us it's the current one.

Starting a new branch

Create new branch:
git branch new-homepage
and switch that as a current branch you work with:
git checkout new-homepage

In the absence of new commits, these two branches are literally identical.
Now the git branch command would return:
   master
* new_homepage

Merging

For example to merge branch where contact info has been updated with master:
git checkout master
git merge update-contact-info
Updating 286af1c...885e3ff
Fast-forward

Three different cases of merge can be identified:

  1. fast forward
  2. merge commit
  3. merge conflict

The first two cases are handled automatically by Git. Only third needs your input, you must select correct line within conflicting file (and remove all angle bracket conflict markers) using text editor. After that add conflicted file to staging area (git add) and finally do the commit (git commit) and push (git push).

 

 REMOTES

If you initialize a new (empty) project at GitHub and then clone it to your computer, remote is called origin by default.

git push origin master
git pull origin master

If there are other people working with same project pushing changes to code, always pull before you push to make sure your own local copy is up to date. Otherwise you can get push rejection, which has to be fixed with pull request anyway.

To see what remotes are defined:

git remote
origin

You can also specify -v, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote:

git remote -v

origin https://github.com/TimThinner/TimThinner.github.io (fetch)
origin https://github.com/TimThinner/TimThinner.github.io (push)

If you have more than one remote, the command lists them all.

To add a new remote Git repository as a shortname you can reference easily, run git remote add <shortname> <url>

 

Want to learn more?
Click here to read more about Git