Objective of this Article
In this article, I’m going to explain, how to do the setup for git repository for existing code base and new repo as well. I have explained git basic command as well, these commands most frequently used in git. In previous article, I have gives basic introduction git, why should use and how to install on a different platform. I personally assist, go through git beginners tutorials first.
Git setup a new repository for an existing code base
1. Create a /path/to/my/codebase/.git directory.
$ cd /path/to/my/codebase $ git init
2. Add config file to GitIgnore
Create .gitignore file and add config file the related to system dependent
3. Add all existing files to the index.
$ git add .
4. Record the pristine state as the first commit in history
$ git add .
Now create a repos on Github or Bitbucket
Now, create a new repository on Github or bitbucket named test. Once you created a repository, you will get a remote URL, It will seem like below. It cloud be SSH or HTTP in this example we are taking HTTPS. URL likes – https://email@example.com/xxxxxxxxx/test.git.
5. Add Remote in Git
Now using this url add the remote in git. Let’s move to terminal and run this command
$ git remote add origin https://firstname.lastname@example.org/xxxxxxxxxx/test.git // replace url to own repo url
Finally, push the all code into server repo
$ git push -u origin master
Let’s summarise what I have did, First I have initialised git using init command, after that I have added all folder file into git using git add . command., then create a repo on server, furthermore I have connected local repo to server repo using git remote add command, Finally push all code into server repo using push command.
The sequence of command is –
$ cd /path/to/my/codebase $ git init $ git add . $ git remote add origin https://email@example.com/xxxxxxxxxx/test.git $ git push -u origin master
Now you completed git setup repository and git setup new repository. Now
Git Basic commands
The following command mostly used in GIT, Every one should be know these command
- Git status
- Git add [-A, -u with options] file name
- Git commit -m “message” [other options]
- Git push origin <branch name>
- Git fetch
- Git merge origin <branch name>
- Git mergetool -t meld
- Git pull origin <branch name>
- Git branch
- Git checkout [option- branch name, filename]
- Git diff [options = –name-only , –no-color etc]
- Git log
- Git show [SHA]
- Git reset [options]
Now we will learn all git command one by one.
$ git status command displays different between staging(stage) and working directory. You can see which changes have been staged, which have not and which files are not being tracked yet by Git. The git status command does not show any information regarding committed history.
$ git status
Git add [-A, -u , file name with options]
$ git add command adds changes working directory to stag area. In other words, that command tells to Git that particular file you want to include for updates in next commit.
SYNOPSIS with Option
$ git add .
It add all files for the next commit.
$ git add -u
It add only tracked files for the next commit.
$ git add <filename>
It stage a particular files for the next commit.
$ git add <directory>
It stage include directory for the next commit.
Git commit -m “message” [other options]
$ git commit -m “message “ command moves all changes to stage from the working area, Only committed changes will move to stage directory.
$ git commit -m "message "
Git push origin <branch name>
$ git push origin <branch name> command is used to upload local(stage repo (mean all committed files)) repository content to a remote repository.
SYNOPSIS with Option
$ git push origin <branch> // for example $ git push origin master
Push all stage content to the specified remote branch. Other words you can say push the specified branch to remote with all commits. git push prevent you from overwriting commits If the local branch has conflict and non-fast-forward merge than server repo won’t accept push
$ git push origin --force
Same as the upper command, but force the push even if it results (has conflict) in a non-fast-forward merge
$ git push origin --all
Using this command you can push all local branch to remote branch.
$ git push origin --tags
Using this command you sends all of your local tags to the remote repository.
$ git fetch command the downloads commits, files, and refs from a remote repository into your stage (local repo) directory.
$ git fetch
Git merge origin <branch name>
$ git merge origin <branch name> command merge the content of local and remote branch
$ git merge origin master
Git pull origin <branch name>
$ git pull origin <branch name> command used to fetch and download content from a remote repository and merge into local repository. In simple words you say git pull works git fetch and git merge job into a single command as simple as that.
$ git pull origin master
The branch is important terms for version control system models. So It’s very important to understand that. Branches are just as pointers to commits. While you create or delete a branch git create and delete pointer. In Simple words git stores a branch as a reference to a commit. That means a branch is a series of commits—it’s not a container for commits.
SYNOPSIS with Options
$ git branch
Display all local branch
$ git branch <branch>
Creates a new branch
$ git branch -d <branch>
Delete a particular branch
$ git branch -D <branch>
Force delete a particular branch
$ git branch -m <branch>
Rename a particular branch
$ git branch -a
Display all remote branch
Git checkout <branch name>
$ git checkout <branch name> command act of switching between different branch. In other words, you say, you can navigate between the branches.
$ git checkout master
$ git checkout -b development
Create and display new branch
$ git diff --color-words
$ git diff-highlight
$ git diff
$ git diff master new_branch ./diff_test.txt
$ git log command is used for displaying commits list. And so many ways to use git log command, you can alter this output using different parameters to git log.
$ git log --graph --oneline --decorate
$ git log --pretty=format:"%cn committed %h on %cd"
$ git log -3
Display commit by Date
$ git log --after="2014-7-1"
$ git log --after="yesterday"
$ git log --after="2014-7-1" --before="2014-7-4"
Show all commit by Author
$ git log --author="John"
$ git log --author="Morris"
Display all commit that contains Message
$ git log --grep="JRA-22:"
Display all commit by commit message
$ git log -- foo.py bar.py
$ git log -S"Hello!"
By Merge Commits
$ git log --no-merges
Only shows commits that not in merge
$ git log --merges
Show the commit that from merge
Git reset [options]
The $ git reset command behaviour same as $ git checkout command. Where $ git checkout completely operates on the HEAD reference pointer, and $ git reset will move the HEAD reference pointer and the current branch ref point
$ git reset --hard
It easy and frequently used option, When you pass –hard argument with reset command the ref pointer is updated to the specified commit, Then Staging and Working Directory is also reset to match that of the specified commit. In simple words working and staging directory permanently deleted all commit after that particular commit.
$ git reset --mixed
While you pass –mixed argument with reset command, that means ref pointers are updated. Staging directory is reset to the state of the specified commit. and any undone commit will move to working directory.
$ git reset --soft
If you use — soft argument then, the ref pointers are updated. The Staging and the Working Directory are left untouched.
Git show [SHA]
$ git show command shows the log message and textual diff. It represented merge commit into special format using different parameter.