git-A simple guide
Learn git commands easily.no more nonsense shit 😉
creating a git repository
Navigate to your working directory and perform below command
to create a local repository
clonning git repository
to clone a local git repository ,use
git clone path/to/repository
in case you want to clone a remote repository ,type below command
git clone https://remote/url/of/repository
how git work
Your local repository maintain three “Trees” which are “working directory” , “index” and “HEAD” .Your working directory holds your original source code file
While Index work as a staging area . You cannot push your any changes without adding your files to INDEX or staging area .
HEAD point to the last commit you have made .Now pushing your source code from INDEX/STAGING area to HEAD is known as commit
When you commit changes made in your source code file then ,only you can push your code to your remote repository which is hosted on github.com
add & commit
To add changes made in your source code file you can use
git add <filename>
git add *
to add all your file to index area at once.
This is first step in git workflow .
Now your files are ready to be commit to the HEAD area.
To commit changes to HEAD use below command
git commit -m "your message"
But still these changes lives in your local repository only .
In next section we see how you can push your source code files to
your remote repository which is hosted on your GitHub .
So make sure you have a github account and a repository created there .
Sign-in for GitHub account is pretty straightforward and it is free .
pushing changes to remote repository
Now changes are made in HEAD .
To push these changes you have to first connect to your remote GitHub repository.
To connect run command below
git remote add origin
Here server is the URL of your remote directory .You can find this on your GitHub repository .
Once our remote connection is established successfully .
Now it time to push our files and changes to our remote GitHub repository .
To push changes to your remote repository you can use
git push origin master
List & removing a remote connection
In above section you see that how we connected and push changes to your remote directory .
Suppose you pasted wrong url of remote repository .
Then what you do ?
You instinct say that run git remote add origin again with new remote repository URL ,but this won’t help .
As you need to remove your remote origin connection and then re-establish it again .
To see your remote origin connection you can use
git remote -v
It return all the remote origin connections .
Now to delete the remote origin connection type this commands in git shell
git remote rm origin
Note that it won’t delete remote directory , rather it delete the reference to the remote directory in your local repository
Branch is a feature of git used to isolate changes from each other . master is a default branch created when we initialize a git repository. We can merge back these branches to master upon completion.
Creating new branch
git checkout -b <new branch name>
Switch back to master
git checkout master
List all local and remote branches
git branch | git branch -a
List all remote branches
git branch -r
To delete branch you can use
git branch -D <branch name>
You can fetch your remote repository branches using
Note that you should already connect to your remote origin
Your branch will not available to other unless you publish it online .use
git push origin <branch name>
Update and merge branches
Update your local repository to the newest commit you have made .execute
To merge your current branch to any of your branch ,you can use
git merge <branch name>