Git is a very popular version control system. Git is open source and free (under gnu public license). Git user gets a full copy of all development history. Changes are added locally and then can be pushed/merged in the repository.
Let’s go over some general concepts on git:
- master is default name of a branch, like when you clone a repository the default local branch is master
- HEAD is a pointer to the current branch.
- origin means remote repository, you can see where it points under .git/config file in your local project.
/home/project/$ git init Initialized empty Git repository in /home/project/.git/ /home/project/$
git init will create an empty repository, basically, you will see a hidden directory .git which has multiple subdirectories.
/home/project/$ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
git add is used to add new files in the staging area (it’s not recorded in the repository until committed). In the below example we created a file “a” and we are adding it to staging area.
/home/project/$ touch a /home/project/$ git add a /home/project/$
git status will show repository status, untracked or uncommitted files. Before starting changes it’s highly recommended to run to check the current state of your environment. Like we added a new file in the above example (but it was not committed). if you run you will see that file is added but not committed.
/home/project/$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: a # /home/project/$
as the command suggests, commit is used to push a file to repo. You might have to run below command to setup username and email.
-m option is used for comments (required)
$ git config --global user.name "Your Name" $ git config --global user.email firstname.lastname@example.org
/home/project/$ git commit a -m "initial commit for a" [master (root-commit) b09792b] initial commit for a Committer: abhishek <email@example.com> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email firstname.lastname@example.org After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a /home/project/$
Now if you run git status again you will see there is nothing to be committed.
/home/project/$ git status # On branch master nothing to commit, working directory clean
git clone is used to checkout/clone an existing repo. Like if you are a new user or if you want to checkout/clone a repo into your environment. Like to clone a repo from “gitlab” you will do the following:
git clone https://gitlab.com/bitarray/ansible.git Cloning into 'ansible'... Username for 'https://gitlab.com': bitarray Password for 'https://email@example.com': remote: Enumerating objects: 86, done. remote: Counting objects: 100% (86/86), done. remote: Compressing objects: 100% (50/50), done. remote: Total 86 (delta 38), reused 58 (delta 27) Unpacking objects: 100% (86/86), done.
Three main uses of diff:
- diff between the current copy and staging
- diff between the staging and local repo.
- diff between the current copy and local repo.
Here is the scenario file “a” has one line “AAA” which is committed to local reposiory.
Now we modify file “a” and replace “AAA” with “BBB”
### diff between local copy vs staging ### since it was nor added there is a difference /home/project/$ git diff a diff --git a/a b/a index 43d5a8e..ba62923 100644 --- a/a +++ b/a @@ -1 +1 @@ -AAA +BBB ## comparing staging with repo ## no differences as we have not added changes to staging. /home/project/$ git diff --cached a /home/project/$ ### difference between local copy and HEAD/repo. ### since change is not commited we see the difference /home/project/$ git diff HEAD a diff --git a/a b/a index 43d5a8e..ba62923 100644 --- a/a +++ b/a @@ -1 +1 @@ -AAA +BBB ### adding changes to staging ### now local copy is commited to staging, hence no difference there /home/project/$ git add a /home/project/$ git diff a
used to ser username and email for commits.
/home/project/$ git config --global user.name bitarray /home/project/$ git config --list user.name=bitarray core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true /home/project/$ git config --global user.email firstname.lastname@example.org /home/project/$ git config --list user.name=bitarray email@example.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true /home/project/$
shows various objects, by default it shows HEAD (which is a pointer to the current branch).
git show <commit_id> ##### show logs for commir_id
$ git show ff95e00960bc16c1528d0ee448240da4624dae06 ### in a more readable format git show ff95e00960bc16c1528d0ee448240da4624dae06 --oneline
shows commit logs. There are many options available or pretty-printing.
/home/project/$ git log --oneline 8fcb246 BBB 6740b98 aaa b09792b initial commit for a
/home/project/$ git log --oneline --shortstat 8fcb246 BBB 1 file changed, 1 insertion(+), 1 deletion(-) 6740b98 aaa 1 file changed, 1 insertion(+) b09792b initial commit for a 1 file changed, 0 insertions(+), 0 deletions(-) /home/project/$
Show git logs for last 3 commits in a readable format (–oneline)
$ git log -3 --oneline ff95e00 added line 2 d8c0e15 deleted b 7b87c99 fff [root@hostmaster project]#
git branch is used to list branches and create a new branch.
/home/project/$ git branch * master /home/project/$ git branch b1 /home/project/$ git branch b1 * master /home/project/$ git branch b2 /home/project/$ git branch b1 b2 * master
in the above example, we created multiple branches b1 and b2. HEAD is pointing to *master. to switch to b1 you can run:
/home/project/$ git checkout b1 Switched to branch 'b1' /home/project/$ git branch * b1 b2 master
At some point during your development cycle you will be required to merge changes from a patch <branchname_patch1> to master (just an example)
### merge changes from two branches
$ git fetch ### to get all updates to your local repo
$ git checkout master ### pulling changes in your work area
$ git merge <branchname_patch1>
git fetch will pull changes and update your local repository (your working copy will not be touched).
git pull is used to update your working copy and local repo. from remote repository. It will merge the new changes or create conflicts when git is not able to merge. You need to be careful when you run pull.
To pull changes from ‘origin’ (which is a remote repository) and ‘master’ (remote branch)
$ git pull origin master
git push origin <branch name >
this is used to push your changes to the remote repository. An example will be: if multiple developers are working in a project and you checkout project from the remote repo for local development. Once you make changes you need to push your changes (ie commit to remote repo.).
### Once you are done committing your local changes and committing to the local repository you can push it to the remote repository. ### bitarray$git push origin master Username for 'https://gitlab.com': bitarray Password for 'https://firstname.lastname@example.org': Counting objects: 127, done. Delta compression using up to 2 threads. Compressing objects: 100% (65/65), done. Writing objects: 100% (65/65), 63.13 KiB | 0 bytes/s, done. Total 65 (delta 52), reused 0 (delta 0) To https://gitlab.com/bitarray/ansible.git ca4205..bdeb343 master -> master
view remote URLS
$ git remote -v
### set remote origin URL
$ git remote set-url origin http//github.com/bitarrayio.git
tagging is used as a bookmark for releases (like you might tag an existing code with v.1.1 like version 1.1 for release purposes.)
### we have one file "a" in the working directory $ cat a BBB ### creating tag t1 $ git tag t1 [project]$ git tag t1 ### now we modify file a (the only file in the project) ### note: we have modified file after tagging ### $ cat a BBB added line 2 $ git commit -m "added line 2" ### tag t1 has the previous file ##you can checkout contents of a tag via $ git checkout tags/t1
git show <tag> ### will show details of a tag.
How to a tag?
$ git tag -d t1 Deleted tag 't1' (was d8c0e15) [project]$
if you delete a file by mistake and want to recover it from repo, use git reset –hard, or if you want to overwrite local changes from the committed change in the repo.
$ git reset --hard
Can be used to rename a file. It should be followed by a commit.
$ git mv <file1> <file2>
$ git rm <filaname> can be used to remove a file from the staging area and local working dir. To remove it from commit you need to run commit after git rm.
Remove all untracked files
$ git clean -f