Many times I have encountered the need to initiate a person on using git. There are many documentations online but many of them are quite extensive described so I will try to make a shorter version for beginners.
I will use Github as a remote server and a real repository as the example.
Short legend:
- Branch – it represents a line of development. Thing of it as an isolated room. You will start from a main room (master) and when you will create a new branch your room will be a clone of the room from where you create the new room: your new room will contain all content from master room at that point
- Commit – it represents a recorded change. Thing of it as a box. You work in a room, you create different things, you put your current work (track changes) in a box that you label it (the commit message).
If you want to have a remote server know your room (branch) content you will have to push the changes.
If you want content from another room you merge with it.
So let’s try to do a Quick Guide by example:
1. Create the repository reference on Github (after creating an account, off course):
Access https://github.com/new, fill out the form and press Create repository (I will use “image” as the repository name)
2. Create a new local directory and init the repository
mkdir image
cd image
git init
3. Create a file
touch README.md
4. Tracking your file
git add README.md
5. Committing your tracked changes
git commit -m "Initial release"
6. Upload your modification on github
git push -u origin master
Note: -u parameter is necessary just on the first branch push so it can set-up stream (associate your branch with the remote one)
7. View status (changes between the last commit and your modifications):
# create a new file and a content
touch my_new_file && echo "my content" > my_new_file
echo "Readme content" > README.md
git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README.md
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# my_new_file
8. Canceling modification done to a file (reset to the last commit state)
git checkout README.md
Note: You can reset all your modification using git checkout .
9. Create a new branch
git checkout -b MY-NEW-BRANCH
10. Reset tracked changes
We will make some changes first so we can have something to reset.:
echo "Readme content\nNew content" > README.md
git add .
# On branch MY-NEW-BRANCH
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: README.md
# new file: my_new_file
#
Now the resetting step:
git reset README.md
# On branch MY-NEW-BRANCH
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: my_new_file
#
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: README.md
#
Note To reset all tracked modifications, use git reset
with no file parameter
12. View logs
First let’s create modification in MY-NEW-BRANCH
git add README.md my_new_file
git commit -m "Add some difference"
The actual view logs:
git log
commit 4d2b83a8946be59cb9924c46c7513d7a2cb07595
Author: Adrian Tilita
Date: Fri Jan 27 15:52:18 2017 +0200
Add some differences
commit ee621d6abd302a0c7d1884d0cc4d43d5d828c083
Author: Adrian Tilita
Date: Thu Jan 26 13:39:55 2017 +0200
Initial release
Note Use git log --oneline
to view just the commit hashes and messages
12. View difference between 2 branches/commits:
git diff master..HEAD
diff --git a/README.md b/README.md
index e69de29..5d87a31 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+Readme content\nNew content
diff --git a/my_new_file b/my_new_file
new file mode 100644
index 0000000..025d08b
--- /dev/null
+++ b/my_new_file
@@ -0,0 +1 @@
+my content
Note: HEAD is the last commit of the current working branch… HEAD could be replaced with MY-NEW-BRANCH in this context.
The same syntax applies on commit hashes. Ex: git diff ee621d6..4d2b83a
11. Merge branches
git checkout master
git merge MY-NEW-BRANCH
12. Reset a un-pushed merge
git reset --hard HEAD~1
Note: The number after HEAD represent the number of commits to reset. You can simple view the difference using git status
and you will receive something like: Your branch is ahead of 'origin/master' by 1 commit.
And that it’s kind of it. Feel free to add questions or leave feedback.
For more information, visit Git official documentation.