Setting up a git repository
- Create a new git repository
2. Creating a bare repository
git init –bare <directoryName>
A bare repo is an empty repo which does not have a working copy. Bare repositories are used as central repositories since it cannot be overridden itself because there is no working copy. Non bare repos are used for developments and bare repos are used as storages.
Use case: A team of developers can work by creating working copies using git init. Once the work is done, a bare repo can be created in a central location and everybody can push their changes to the bare repo.
3. Cloning a project
git clone <repo>
git clone <repo> <directory>
4. Repo-To-Repo Collaboration
Unlike SVN git does not have a difference in a central repo and a client repo. Any two repos can communicate to each other.
5. Set configurations for a git Repo
git config –global –user.name <pathmasri>
git config –global user.email <email>
git config –global alias.<alias-name> <git-command>
git config –system core.editor <editor>
git config –global –edit
1. Adding files to staging area
git add <fileName>
2.Adding folders to staging area
git add <folderName>
3. Interactive adding
git add -p
4. Commiting the code
git commit -m <commit message>
git commit -a
This will directly commit all changes if the modified files are tracked using git add, at least once at some point of their history.
Inspecting a repository
1. List which files are staged and unstated,untracked
2. To ignore files committing you need to add them in .gitignore file.
– You can keep your .gitignore file globally or inside the project root folder or inside any folder in the project.
– .gitignore file should also be commited to the project
3. Displaying the committed snap shots
git log -n <number of commits>
git log -n 3
git log –oneline
shows the commit only a single line
git log –stat
shows the changed files in each commit
git log -p
shows the most detailed description about each commit
git log <since>..<when>
Parameters can be a branch or a commit id or any other reference
git log <file>
changes done to file
git log –author=”John Smith” -p hello.py
git log –oneline master..some-feature
Viewing old commits
1. Git checkout
Three things can be checked out => commits,files,branches
– git checkout master
– git checkout <commit> <file>
At this stage, you can commit the file with current state. git status will show you the change of the file.
– git checkout <commit>
This will lead to a detached Head state. No changes can be done and committed at this stage.
2. Detached HEAD – Head usually points a branch. When you check out an old commit, HEAD will not longer point to a branch. This state is called a detached head state.
1. Reverting changes
git revert <commitid>
This will revert the particular commit and new commit id will be generated for the new commit
2. Resetting Changes
git reset – This is a permanent undo
git reset <file> – Remove the specified file from staging area. Nothing will be overwritten
git reset – Unstaging all the changes. Working copy will not be overwritten
git reset –hard – Unstage all changes and overwrite the working copy to match with the last commit
git reset <commit>
Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone. All changes made since <commit> will reside in the working directory, which lets you re-commit the project history using cleaner, more atomic snapshots.
git reset <commit> –hard
Move the current branch tip backward to <commit> and reset both the staging area and the working directory to match. This obliterates not only the uncommitted changes, but all commits after <commit>, as well.
3. Clean command
git clean -n
This is a dry run. Command will show you the files which are going to get deleted.
git clean -f
Clean all untracked files excluding the igonored file using .gitignore
git clean -f <path>
Cleann all the untracked files in a specified path
git clean -df
Clean all the files and directories
git clean -xf
Clean all files including the files ignored files.
Re-writing the history
git commit –amend
This will allow to combine the staged changes with the recently committed changes.
If there is nothing in staging area, this will just edit the commit message
This should not be done on commits are that are already pushed to a public repo.
git rebase <base>
This will help to maintain a linear project history
Frequently Used Git Commands
Checkout a remote branch
git checkout –track origin/branchName
Checkout a tag
git checkout tags/tagName
Check the current tag
git describe –tags
Comparing two tags
git log –oneline tag1…tag2
git diff –name-only tag1…tag2
Create a new branch
git branch <branchName>
Create a new branch and checkout from the same
git branch -b <branchName>
Merge branchB to branchA
git checkout <branchA>
git merge <branchB>
Merge a particular commit
git cherry-pick <commitId>