Post

[Git Commands] Push, Fetch, Merge, Stash, and Reset

Git

1. Git Push

1
2
git push --set-upstream origin <branch>
git push -u origin <branch>
  • -u option is the same effect as --set-upstream

By setting the upstream branch, you specify the default remote branch

2. Git Fetch

1
git fetch
  • git pull = git fetch + git merge

  • Retrieve updates from a remote repo, but without changes into your current workspace

3. Git Merge & Rebase

Both Git merge and Git rebase are used to integrate changes from dev branch into main branch

But, they do it in different ways and the result is a different commmit history.

  • Git Rebase provides a more linear project history by avoiding unnecessary merge commits.

  • Git Merge keeps all commits while making visible when work started on different branches (simple but potentially messy)

4. Git Stash

It is a command that allows you to temporarily save changes that you have made but do not want to commmit yet.

When?

  • If you have uncommitted changes in your working directory that conflict with the changes you’re trying to pull or merge

  • Before switching branches

A. Stashing Changes

1
2
git stash
git stash "commit message"

B. Apply stash and drop

  • apply a specifiy stash & drop
1
2
git stash apply stash@{1 ~ n}
git stash drop stash@{1 ~ n}

C. Apply a recent stash & drop

1
2
git stash apply
git stash drop

D. Stash pop = apply + drop

1
git stash pop

5. Git Reset

It is used to undo the last commit in your repository

1
git reset HEAD^
This post is licensed under CC BY 4.0 by the author.