Skip to content

Git Commands Memo

Stanislav Osipov edited this page Aug 24, 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 clean -fd - Removes untracked files & directories.
  • 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.

Reset local repository branch to be just like remote repository HEAD

git fetch origin
git reset --hard origin/master

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.

Cherry Pick

When should I use cherry-pick?

The short answer is: as rarely as possible. The reason why you should use cherry-pick rarely is that it easily creates "duplicate" commits: when you integrate a commit into your HEAD branch using cherry-pick, Git has to create a new commit with the exact same contents. It is, however, a completely new commit object with its own, new SHA identifier.

Whenever you can use a traditional Merge or Rebase to integrate, you should do so. Cherry-pick should be reserved for cases where this is not possible, e.g. when a Hotfix has to be created or when you want to save just one/few commits from an otherwise abandoned branch.

You only need to provide the SHA identifier of the commit you want to integrate into your current HEAD branch:

  • git cherry-pick af02e0b

You can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them:

  • git cherry-pick af02e0b --no-commit

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 cherry-pick F would be:

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

Submodules

If it's the first time you check-out a repo you need to use --init first.

  • git submodule update --init --recursive

For git 1.8.2 or above, the option --remote was added to support updating to latest tips of remote branches:

  • git submodule update --recursive --remote

For git 1.7.3 or above you can use

  • git pull --recurse-submodules

if you want to pull your submodules to the latest commits instead of the current commit the repo points to. See git-submodule for details

Tags

Create Tag For Last Commit git tag <tag_name> HEAD (for the last commit) git tag <tag_name> HEAD~1 (for the commit before HEAD) git tag <tag_name> HEAD~2 (for two commits before HEAD)

Delete a tag from remote repo

git tag -d [tag];
git push origin :[tag]

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