Learn basic git commands on UNIX Platform: Git is distribution revision control system. It is fast, scalable and has rich command sets. In this post, I will explain how we can use git to do revision control on UNIX platform.
Create git repository
When you work on any project and do version control, first thing you do is create repository. Following are the commands you have to use to create repository.
Create a directory.
Set into the directory.
Initialize the repository.
After initialization run git status command to check the status. You will get below response.
>git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
Add file using git add
This is an empty repo. You can start creating your programs in side this repo.
After creating your first program run git status.
>git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) my_program.sh nothing added to commit but untracked files present (use "git add" to track)
This will show you untracked files. So, to include the file run git add.
>git add my_program.sh
After addition, again run git status.
>git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: my_program.sh
Above status shows new file is added but yet not committed.
Committing a change in git repository
In order to commit this addition to repository run below command.
>git commit -m "Adding first program file" [master (root-commit) 975175a] Adding first program file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 my_program.sh
After you run git commit command, check the status again.
>git status On branch master nothing to commit, working tree clean
Finally, it show nothing to commit, as we have already committed the addition of new program file in the repository.
Check git history
If you want to check the history, just run git log.
>git log commit 975175a6526ef5c5262bdb800caf361ce9f7e013 (HEAD -> master) Author: Techies Answer <firstname.lastname@example.org> Date: Sun Sep 9 00:17:24 2018 +0530
Cloning a git repository
You can clone the repository which you have created by using git clone.
>git clone file:///home/satya/my_repo Cloning into 'my_repo'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done.
If you get new requirement to modify script then in the new repository modify your script and commit the change.
vi my_program.sh # modified the script as per new requirement.
Now check with git status. Here it will show script has been modified. If you want to keep the changed script then use git add or else run git checkout to discard the change in working directory.
git status On branch master Your branch is up to date with 'origin/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: my_program.sh no changes added to commit (use "git add" and/or "git commit -a")
I have run git add to keep the change in git staging environment and then ran git commit with appropriate message to commit the change in git repository.
>git add my_program.sh
>git commit -m "Modified the program as per new requirement" [master 17505f4] Modified the program as per new requirement 1 file changed, 1 insertion(+) >git status Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
Here, it shows branch is ahead from the origin master repository, from where we have cloned this repo. So to make origin repository upto date we will run git push. Before that run git pull. This will make sure your current repository is upto date. This we do in case somebody might have committed some changes in your origin repository.
>git pull Already up to date.
You can see its already updated. That means no one has committed any change to origin repository after you have cloned. Now you can safely push your changes to origin repository from current repo.
>git push Counting objects: 3, done. Writing objects: 100% (3/3), 297 bytes | 297.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: is denied, because it will make the index and work tree inconsistent remote: with what you pushed, and will require 'git reset --hard' to match remote: the work tree to HEAD. remote: remote: You can set the 'receive.denyCurrentBranch' configuration variable remote: to 'ignore' or 'warn' in the remote repository to allow pushing into remote: its current branch; however, this is not recommended unless you remote: arranged to update its work tree to match what you pushed in some remote: other way. remote: remote: To squelch this message and still keep the default behavior, set remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To file:///home/satya/my_repo ! [remote rejected] master -> master (branch is currently checked out) error: failed to push some refs to 'file:///home/satya/my_repo'
Here, I got error. This is because by default updating non bare repository is denied.
Reason is, it will make the index and work tree inconsistent.
You can use below command to ignore this and go ahead with git push. But this is not recommended unless you know things and make sure work tree is updated either side.
git config --global receive.denyCurrentBranch updateInstead
Now there is no problem in running git push.
>git push Counting objects: 3, done. Writing objects: 100% (3/3), 297 bytes | 297.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To file:///home/satya/my_repo 975175a..eca6373 master -> master
Remove file from git repo
To remove any file just use rm command to remove file.
Git status command will show file deleted. At this stage file is deleted from local disk but it is not deleted from git repository.
>git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: my_program.sh no changes added to commit (use "git add" and/or "git commit -a")
To remove file from git repository use git rm command, and then run git commit.
git rm my_program.sh git commit -m "removed my program" [master 1bf0668] removed my program 1 file changed, 1 deletion(-) delete mode 100644 my_program.sh
And finally, to push this change run git push.
git push Counting objects: 2, done. Writing objects: 100% (2/2), 204 bytes | 204.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0) To file:///home/satya/my_repo eca6373..1bf0668 master -> master