Skip to main content

Git

Creating and Switching Branches

Create a Branch

git branch [branch_name]

Create a Branch and Switch to It Immediately

git checkout -b [branch_name]

Switch Branches

git checkout [branch_name]

List All Branches

git branch

Renaming Local Branches

Use the -m flag to change the name of the branch.

Rename the Current Branch

git branch -m [new_branch_name]

Rename a Branch While on Any Branch

git branch -m [old_branch_name] [new_branch_name]

Adding Files

Add All Files to the Staging Area

git add .

Add Specific Files to the Staging Area

To add all files starting with fil to the staging area:

git add fil*

Committing Changes

This command will open a text editor in the terminal where you can write a full commit message:

git commit

Add a commit message without opening the editor. This command lets you specify a short summary for your commit message:

git commit -m "Commit message"

Push an Empty Commit

git commit --allow-empty -m "[TICKET-ID] Relevant commit message here"

Merging

Merge a Branch into the Current Branch

git merge [branch_name]

Abort a Merge

git merge --abort

Deleting Branches

Delete a Branch

git branch -d [branch_name]

Delete All Local Branches Except Certain Ones

To delete all branches except master and develop:

git branch | grep -v "master" | grep -v "develop" | xargs git branch -D

Pushing Changes to a Remote Repository

git push

Pulling Changes from a Remote Repository

git pull

GitLab Not Automatically Pulling

When you try to pull from the remote repository and it's not done automatically, you might see the following prompt:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> <current_branch>

To ensure automatic pulling, use the following command:

git config --global --add --bool push.autoSetupRemote true

Then, follow with:

git push

After this, you should be able to use the git pull command again without manually linking the remote branch.

Reconcile Divergent Branches

If you forget to pull new changes on your branch, make new commits, and want to push them, you can't proceed because of divergent branches. This can be fixed by doing a variant of git pull:

git pull --no-ff

Fetching Remote Repository Changes

git fetch

Rebuild Node After Switching Node Version

npm rebuild node-sass

Sorting Branches by Date

git branch --sort=-committerdate

Removing Cached Files

git rm --cached -f *.DS_Store

Viewing a Short Summary of Git Commits

Short Summary of All Commits

git log --oneline

Short Summary of All Commits Without Merges

git log --oneline --no-merges

Cherry-Picking

Cherry-pick allows you to select individual commits and integrate them from any branch into your current HEAD branch. This is useful when you need to release one item from a list of other changes in the develop branch.

The usual workflow is: after your commits are merged into develop, you create a branch from master, then cherry-pick your commits to this new branch, review the code, and release it to master.

git cherry-pick [SHA identifier of the commit you want to integrate]

or

git cherry-pick -m
git push

Creating Git Aliases

Set Alias

Example 1: Setting alias from git commit to git ci

git config --global alias.ci commit

Example 2: Setting alias from git checkout to git co

git config --global alias.co checkout

Show All Git Aliases

git config --list | grep alias

Useful Aliases

[alias]
co = checkout
cob = checkout -b
coo = !git fetch && git checkout
br = branch
brd = branch -d
st = status
aa = add -A .
unstage = reset --soft HEAD^
cm = commit -m
amend = commit --amend -m
fix = commit --fixup
undo = reset HEAD~1
rv = revert
cp = cherry-pick
pu = !git push origin `git branch --show-current`
fush = push -f
mg = merge --no-ff
rb = rebase
rbc = rebase --continue
rba = rebase --abort
rbs = rebase --skip
rom = !git fetch && git rebase -i origin/master --autosquash
save = stash push
pop = stash pop
apply = stash apply
rl = reflog

Resources