Skip to content

Git Commands Memo

Stanislav Osipov edited this page May 6, 2020 · 20 revisions

A small article to help you with day to day Git commands and common problems.

Day to day commands

  • git status - Your current repository status.
  • git clean -f - Removes untracked files.
  • git reset --hard - Discard all local changes.
  • git reset --hard origin/master - Discard all local changes & local commits.
  • git add -A - Add all untracked fiels.
  • git commit -am "<commit_message>” - Commit.

Branches

  • git checkout -b <branch_name> master - Create feature branch of master. Master branch name is optional if currently inside the master branch.
  • git checkout <branch_name> - Switch to the branch .
  • git push -u origin <branch_name> - Push your branch to the remote repository.

Delete a branch

  • git branch -d <branch> - Delete a branch on your local filesystem.
  • git push origin <branch> - Delete a branch on the remote repository.

Rebase

git pull --rebase origin master - Base your commits on top of the original branch.

It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolve any such merge failure and run git rebase --continue. Another option:

  • git rebase --skip - To bypass the commit that caused the merge failure.
  • git rebase --abort - Abort the rebase process.

Assume the following history exists and the current branch is "topic":

      A---B---C topic
     /
D---E---F---G master

From this point, the result of the following command git pull --rebase origin master would be:

              A'--B'--C' topic
             /
D---E---F---G master
  • git push -f - Once you are done with local rebase, you need to force push the changed branch to the remote repository.

Common problems

The solution to some well-know mistakes when working with git-flow.

How do I move un-pushed master committed code to another branch?

  • git checkout -b new-branch - Create a new branch that has the current state.
  • 
git checkout master - Go back to the branch you want to remove the un-pushed commits from.
  • 
git reset --hard origin - Remove the un-pushed commits from the master.

Git does not see the files I added

  1. Check the repository .gitignore.
  2. Check your global .gitignore.
  • Windows cmd %USERPROFILE%\.gitignore
  • *nix or Windows git bash: open ~/.gitignore_global