Git for Beginners

I am quite new to Git. I just would like to have something simple to control my configuration files, and paper drafts. And I think Git is the perfect thing to apply. It might be a little bit painful to learn, but you will be paid back. When I started, I searched a lot for what I should do. So, after a while, I put some notes here as Git for beginners. Hopefully, it can help you to learn Git more quickly and get rid of the annoying lost of control in your files.

Git Resources

I heard about Git for quite a long time, but I never used it. Until one day, I met this open course How to Use Git and GitHub. It is short and compact for you to have a nice start. You may also know some logic theory in the application of Git through this course. So, if you have time, take it.

  • You may visit the official website of Git, where you can download the latest version of Git and other useful information.
  • You may find nice information about the git commands from Git Cheat Sheet from GitHub and Git Cheat Sheet from TOWER. Actually they are two git control software you may use.
  • However, for softwares, I highly recommend Source Tree. It is nice and support both Mac and Windows. Most of all, it is free.
  • You can find more options about GUI clients. With these clients, you can save yourself from the terminal and command lines.

Git Configurations

Set your “user name”.

git config --global user.name "user name"

Set your “user email address”

git config --global user.email "user email address"

Enable the colour highlight in the terminal command lines.

git config --global color.ui auto

Common Commands

Create an empty folder named “XX”.

mkdir XX
pwd

Git commands

Initialise

Initialise a repository in the current folder named .git, which is commonly invisible. Beginners would better not touch this file unless you know what you are doing.

git init

Clone

Clone an existing repository from web server at “url”, for instance, GitHub.

git clone "url"

Add changes

Add the changes related to file “XX” into the temporary staging area.

git add XX

Commit

Commit the changes you have just made. It is recommended to commit for each logical change.

git commit -m "Commit content"

Check status

Check the status of your repository. You may find how many files are changed in the temporary staging area.

git status

Compare differences

Compare the differences between file1 and file2. You may directly use git diff filename to see the latest changes of “filename”.

git diff file1 file2

Check history

Check the history from the newest to the oldest.

git log

Reset after

Undo all commits after “commit number”. But all the changes are still locally stored.

git reset "commit number"

Reset all

Discard all the changes and reset the files best to the “commit number”.

git reset --hard "commit number"

Reset to latest

Reset files back to the last version. If you would like to reset your files to the last n version, you may use git reset --hard HEAD~n.

git reset --hard HEAD^
git reset --hard HEAD~

Delete git and local

Delete the file named “XX”.

git rm XX

Delete git but keep local

Remove the file named “XX” from the version control but keep it in local.

git rm --cached "XX"

List a branch

List all branches in the current repository.

git branch

Create a new branch

Create a new branch for development named “XX”.

git branch "XX"

Switch to a branch

Switch to the specified branch named “XX”.

git checkout "XX"

Create and switch to a new branch

Crate and switch to a new branch named “XX”.

git checkout -b "XX"

Delete a branch

Delete a branch named “XX”.

git branch -d XX

Merge

Merge two branches.

git merge file1 file2

Add a remote repository

Add a remote repository, which could be your GitHub.

git remote add origin https://github.com/jialunliu/XXX

Check the remote repository

Check status of your remote repository.

git remote

Push

Push your master branch to the remote repository. You have to add -u for the first push, after which you can move it.

git push –u origin master

Clone from a remote repository

Clone one existing repository form the remote server.

git clone https://github.com/jialunliu/XXX

Stash

If you want to leave the temporary files for now and come back later for comments, you may use the stash.

You may hide the current files with:

git stash

You may see all the hidden files with:

git stash list

You may recover the stashed files without deleting them with:

git stash apply

You may delete the stashed files with:

git stash drop

You may recover and delete the files with:

git stash pop

Ignore

If you want to ignore some files, for example the latex related files, you can edit the “.gitignore” file. Add lines of the file type that you do not want to commit in that file.

So, that is it. Now you are not a green hand any more. Say goodbye to “beginner”.

That is it.
Cheers!