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.
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 config --global user.name "user name"
git config --global user.email "user email address"
git config --global color.ui auto
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.
Clone an existing repository from web server at “url”, for instance, GitHub.
git clone "url"
Add the changes related to file “XX” into the temporary staging area.
git add XX
Commit the changes you have just made. It is recommended to commit for each logical change.
git commit -m "Commit content"
Check the status of your repository. You may find how many files are changed in the temporary staging area.
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 the history from the newest to the oldest.
Undo all commits after “commit number”. But all the changes are still locally stored.
git reset "commit number"
Discard all the changes and reset the files best to the “commit number”.
git reset --hard "commit number"
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^
Delete the file named “XX”.
git rm XX
Remove the file named “XX” from the version control but keep it in local.
git rm --cached "XX"
List all branches in the current repository.
Create a new branch for development named “XX”.
git branch "XX"
Switch to the specified branch named “XX”.
git checkout "XX"
Crate and switch to a new branch named “XX”.
git checkout -b "XX"
Delete a branch named “XX”.
git branch -d XX
Merge two branches.
git merge file1 file2
Add a remote repository, which could be your GitHub.
git remote add origin https://github.com/jialunliu/XXX
Check status of your remote repository.
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 one existing repository form the remote server.
git clone https://github.com/jialunliu/XXX
If you want to leave the temporary files for now and come back later for comments, you may use the
You may hide the current files with:
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
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.