Login to your github/bitbucket/gitlab account and create new repository
When you create your repository you will get this
You can also clone this repository to your local then manage your project. Suppose we want to start a new django project for git practice.
First go to your desired location in local.
#Git 2
How to push your first commit in GIT?
In the empty project I will add a django project mysite. Lets do it.
Let check and add the git commit file
Now commit and push the added file
After sucessfully push you will find this
Let add a README file
#Git 3
How to create new branch and feature development branch in Git?
In professional development we need to create minimum three major branch must.
Branches name should like this
master - Production Server
stage - QA/Staging Server
develop - Developer default local server
Lets create this major branches
Now we are in develop branch. Lets run the django project.
We want to develop a django app polls. This is one of feature of this project. So we should make a feature branch
copy from develop branch.
New branch name should be in this sequence username/token/branch-name-by-hyphen.
Here
username - Owner of the branch
token - Branch type
branch-name-by-hyphen - Branch name
Token
feat - A feature branch
hotfix - Hotfix changes for production issues
bugfix - A bugfix branch
chore - Cleaning up / organizing the code
wip - Branched out for some WIP stuff that has to be pushed
You can checkout this link for the convention of branch name.
Convention
Lets create poll app feature branch.
Rest of the Polls app part, you can follow DJango official tutorial from this link
Polls
Now commit and push this polls app.
#Git 4
How to merge branch in Git?
We will merge mahfuz110244/feat/poll-app branch to develop.
#Git 5
How to save username and password in Git?
Every time we need to give credential when we push any commit to remote. We can save our username and password
using below git command.
#Git 6
How to unstage any added file in Git?
In our simple poll app, we’ll create two models: Question and Choice. A Question has a question and a publication date.
A Choice has two fields: the text of the choice and a vote tally. Each Choice is associated with a Question.
#Git 7
How to change the last commit log message in Git?
Let’s say you just committed and you made a mistake in your commit log message. So we can change the commit log message
using git amend command until push to the remote.
Sometimes we forget to add any file in the commit. But after commit we can add the missing file using git amend command.
#Git 8
How to rollback to last commit push or any commit push in Git?
First you need to know the last push commit id or desired commit id HEAD No that you want to rollback.
Using git reflog you can get your HEAD No.
Reflog
Reference logs, or “reflogs” are a mechanism Git uses to record updates applied to tips of branches and other commit references.
Reflog allows you to go back to commits even though they are not referenced by any branch or tag. After rewriting history,
the reflog contains information about the old state of branches and allows you to go back to that state if necessary.
Every time your branch tip is updated for any reason (by switching branches, pulling in new changes, rewriting history or simply
by adding new commits), a new entry will be added to the reflog.
Now if you want to rollback your last push
If you want to rollback HEAD No 6 e2dac0a this commit just replace the HEAD No
#Git 9
How to switch branch without commit in Git?
Sometimes we need to switch between branches because of we don’t want to commit this or we have no time because of some urgent
work of different branch. We can do that using git stash command.
Stash
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts
them from your working copy.
Git Stash Pop
You can reapply previously stashed changes with git stash pop. Popping your stash removes the changes from your stash and reapplies them to your working copy.
Git Stash Apply
Alternatively, you can reapply the changes to your working copy and keep them in your stash with git stash apply
Suppose you have stash multiple times. You need to go to specific stash. Suppose you have two stash and you want to pop stash no 1.
#Git 10
How to delete a Git branch locally and remotely?
Sometimes we make mistake in branch creating like we need to create a new branch that copy from master branch but
mistakely we have created branch from develop. So we need to delete that branch from locally. If we push this
branch in remote then we also need to delete branch from remote.
The -d option is an alias for –delete, which only deletes the branch if it has already been fully merged
in its upstream branch. You could also use -D, which is an alias for –delete –force,
which deletes the branch “irrespective of its merged status.”
#Git 11
Git Rebase
According to the git documentation, the rebase command will reapply commits on top of another base tip. This definition might be a little daunting.
It’s easier to explain rebase as a procedure that adds the changes of the current branch to the tail of another branch.
The primary reason for Git rebasing is to maintain a linear project history. In rebase, you change your branch that’s being rebased so it looks
like it was branched off a new base instead of the original. The process rewrites the commits, so you’ll end up with different commit IDs.
The golden rule of git rebase is to never use it on public branches.
Merge takes all the changes in one branch and merges them into another branch in one commit.
Rebase says I want the point at which I branched to move to a new starting point
So when do you use either one?
Merge
Let’s say you have created a branch for the purpose of developing a single feature. When you want to bring those changes back to master,
you probably want merge (you don’t care about maintaining all of the interim commits).
Rebase
A second scenario would be if you started doing some development feature branch and then another developer made an unrelated change and merge that to
base branch. Rebase is quite often a good idea to do before merging because you can resolve any conflict in your feature branch with base branch.
Git Rebasing Example
In this example, we will first create a test case with master and feature branch. Then we will do a standard merge. Next, we will recreate the test case and perform rebase and merge.
Creating Master and Feature Branches
Here is the scenario we will create:
In the above example, we are taking the following path:
Simple Merge
Let’s use the log command to check both branches.
In the ‘master’ branch, you will notice there is a new commit G that has merged the changes from ‘feature’ branch.
Basically, the following action has taken place:
In the Commit G, all the changes from feature branch have been brought into the master branch.
But the feature branch itself has remained untouched due to the merge process. Notice the hash of each commit.
After the merge, E (7c5c85e) and F (0286690) commit has the same hash on the feature and master branch.
Merging with Rebasing
Let’s repeat step 1 to create the master and feature branches again.
Notice that after the rebase and merge both branches are the same. Also, the hashes for E and F have changed in both branches. Basically, in the rebase scenario, this is what happened:
That’s why there is no new commit. The E and F commits have been recalculated and latched to the end of the ‘master’ branch.
Rebasing is a useful tool when you want to clean up the history of your work. However, there is a danger which has given birth to the golden rule.
Cherry picking in Git means to choose a commit from one branch and apply it onto another. When you are working with a team of developers on a medium
to large sized project, managing the changes between a number of git branches can become a complex task. Sometimes you don’t want to merge a whole
branch into another, and only need to pick one or two specific commits. This process is called cherry picking.
When should we use Cherry Picking?
Let’s say you are working in an project where you are making changes in a branch called new-features. You have already made a few commits but want to
move just one of them into the master branch. From new-features branch run git log –oneline to get a better log of your commits history. Note that
the commit id is what we need to start the cherry picking.
Caution!
Cherry picking is commonly discouraged in developer community. The main reason is because it creates a duplicate commit with the same changes
and you lose the ability to track the history of the original commit. If you can merge, then you should use that instead of cherry picking.
Use it with caution!