Just some short notes to remember how to get started with Git.
Check version
$ git --version
Setting-up
Set the following configuration items if using Git for the first time, as these values are used for each commit made.
$ git config --global user.name "Your Name" $ git config --global user.email "[email protected]"
Initializing a new Git repository
Make sure you are in the top-level folder of your project and then run the following command.
$ git init
Create the .gitignore file
Git has a special file called ‘.gitignore’ that allows you to specify which files to NOT include in your repository. The following file is based on the recommended .gitignore file for Python from GitHub.
# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # PyCharm files .idea/ # Instance Folder - used for sensitive configuration parameters /instance
Setting remote repository
Make sure you are in the top-level folder of your project and set up the remote repository.
$ git remote add origin [email protected]:nidkil/name-of-repository.git
Check the remote repository has been set correctly.
$ git remote -v
Staging files
The standard flow for adding files to your git repository is to create/edit the files, add them to the staging area, and then commit them to your repository.
$ git status $ git add . $ git status
Initial commit
The following command makes the first commit to the repository, including a message describing the commit.
git commit -m "Initial version"
You can confirm that the commit was successful by checking the log of the repository.
git log
Push to the remote repository
To push the local Git repository to the remote Git repository.
$ git push -u origin master
Working with a feature branch
A common method of developing features is to create a feature branch where you implement specific functionality and then merge the changes back into the master or development branch. Follow the process: create a new feature branch, make changes and merge changes back into the ‘master’ branch.
Create a new feature branch.
$ git checkout -b name_of_feature_branch
Check new feature branch was created (the star next to ‘name_of_feature_branch’ branch indicates it is the current branch that we are working in).
$ git branch
The following commands stage, commit and merge the changes into the local repository.
$ git add . $ git status $ git commit -m "Added use of templates and Bootstrap" $ git checkout master $ git merge add_templates
Delete the branch.
$ git branch -d add_templates
Add a version tag to your update.
$ git tag -a v0.1 -m "version 0.1"
You can view the changes of a version tag.
$ git show v0.1
You can list tags.
$ git tag
Push the changes into the remote repository.
$ git push -u origin master
Check difference between local and remote repository.
$ git diff master origin/master
Overwrite local master with remote master.
$ git fetch --all $ git reset --hard origin/master
Create a branch of the local master before overwriting it with the remote master.
$ git checkout master $ git branch $ git fetch origin master $ git reset --hard origin/master
After this, all of the old commits will be kept in the . However, uncommitted changes (even staged) will be lost. Make sure to stash and commit anything you need.
Oops working in master instead of a branch
Happens to me a lot I start working on a change and after a while realize I am working on my local master instead of a feature branch. Continue working or through away your changes and start again in a new feature branch. Neh, just checkout the new feature branch and the changes will automatically move to it.
$ git checkout -b name_of_feature_branch
Just do a git branch
and git status
to see that it actually worked 🙂
Cool isn’t it? Who doesn’t love git?