The merge command is used to bring together changes from two branches. For example, if you have a feature branch that is branched off of the master branch. When you are done with that feature branch you can use the merge command to merge it back to master.
git checkout receiving-branch git merge feature-branch git push origin receiving-branch
We will assume receiving-branch to be master for the example above.
The merge command will bring all the commits from the feature branch into the master branch.
If you want to revert the merge you have started. Use the following command:
git merge --abort
The rebase command is used to bring changes from another branch, usually a branch from which the current branch migrated from. It is basically putting the changes on the base and not on top.
git rebase commit-id(from the parent branch)
You can always rebase from master which is the source of truth.
# 1 - pull latest code from master git checkout out master git pull --rebase # 2 - checkout out feature branch git checkout create-swipe # 3 - rebase feature branch from master git rebase master
If you want to revert rebase in progress you run this command:
git rebase --abort
git pull
is used to fetch/download new content from a remote repository and immediately update the local repository with that content. git pull
is a combination of git fetch
and git merge.
In the first step of operation git pull
will execute a git fetch
scoped to the local branch that HEAD
is pointed at. Once the content is downloaded, git pull
will enter a merge workflow. A new merge commit will be-created and HEAD
updated to point at the new commit.
// update local branch with new commits from the remote repo. git pull // update local branch with new commits from the remote. New commits from remote will be applied under new changes on local. // In other words, your local changes will be on top of the new commits from remote. git pull --rebase
git fetch
vs git pull
git fetch
is a safe option whereas, git pull
is a unsafe option. git fetch
will download the remote content but not alter the state of the local repository. git pull
will download remote content and immediately appy the remote changes to local state of the local repository.
git push
is used to upload local repository commits/changes to a remote repository. This is how you transfer commits from your local repository to a remote repo. It’s the counterpart to git pull
or git fetch.
Pushing has the potential to overwrite changes so you have to be careful when pushing.
// push local commits/changes to remote git push // force the push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing. git push --force
git commit
is used to take a snapshot of the project’s currently staged changes. Commits are the core building block units of a Git project timeline. Commits are created with the git commit
command to capture the state of a project at that point in time. Git Snapshots are always committed to the local repository.
You can have multiple commits on local. When you are ready, you can then push your local commits to remote with git push
// add certain files(index.html) to commit git add index.html // or add all updated files to commit git add . // commit a snapshot of all changes in local repo git commit -a //By default, git commit will open up the locally configured text editor to add a commit message. //Passing the -m option will forgo the text editor prompt in-favor of an inline message. git commit -m "setup" // create a commit of all the staged changes and takes an inline commit message. // shortcut for (git add .) + (git commit -m "message") git commit -am "setup"