Git Merge and Push
1. Git merge
git merge <branch> brings the changes from <branch> into the branch you are currently on.
git checkout main # go to the target branch
git merge feature # bring feature into main
Fast-forward merge
Happens when main has no new commits since feature was created. Git only moves the main pointer forward.
Before:
A---B---C <- main (HEAD)
\
D---E---F <- feature
After:
A---B---C---D---E---F <- main (HEAD), feature
Three-way merge
Happens when both branches have new commits. Git normally creates a merge commit (M).
Before:
A---B---C---G <- main (HEAD)
\
D---E---F <- feature
After:
A---B---C---G-------M <- main (HEAD)
\ /
D---E---F <- feature
Merge conflict
Happens when both branches change the same lines differently.
git status # list conflicted files
# edit each file and remove the <<<<<<< ======= >>>>>>> markers
git add <file>
git commit # complete the merge
git merge --abort # or cancel the merge
Official docs: git merge
2. Push to a remote the first time
Do this after committing your local changes. It is simplest if the remote repository is empty.
git remote add origin https://github.com/direct-devops/sample_git.git
git remote -v # verify
git branch # check if your branch is main or master
git push -u origin main # use master if that is your branch
originis only the conventional nickname for the remote. You can use another, such asdirect-devops, and then rungit push -u direct-devops main.-u(same as--set-upstream) links the local branch to the remote branch.- If
origin already exists, rungit remote set-url origin <url>.
3. Push a feature or release branch
git checkout -b release-1.2.0 # create (omit -b if it exists)
git push -u origin release-1.2.0 # first push
git push # later pushes
git push release-1.2.0does not work for this, because Git readsrelease-1.2.0as a remote name. Usegit pushorgit push origin release-1.2.0.
4. Task: create branches, merge and push
Start in a repository that has at least one commit on main and has origin configured.
Step 1: Create the first branch
git checkout main
git checkout -b feature-login
echo "login page" > login.txt
git add login.txt
git commit -m "Add login page"
Step 2: Create the second branch from main
git checkout main
git checkout -b feature-payment
echo "payment page" > payment.txt
git add payment.txt
git commit -m "Add payment page"
Step 3: Merge both into main
git checkout main
git merge feature-login # fast-forward
git merge --no-edit feature-payment # merge commit
Step 4: Verify
git log --oneline --graph --all
Step 5: Push
git push -u origin main
git push -u origin feature-login feature-payment
Step 6 (optional): Delete merged branches
git branch -d feature-login feature-payment
git push origin --delete feature-login feature-payment
Quick reference
| Command | Purpose |
|---|---|
git branch -a |
List local and remote-tracking branches |
git checkout -b <name> |
Create and switch to a branch |
git merge <branch> |
Merge a branch into the current one |
git merge --no-ff <branch> |
Force a merge commit |
git remote -v |
Show remotes |
git push -u origin <branch> |
First push of a branch |
