Skip to content

Instantly share code, notes, and snippets.

@gwang
Last active January 3, 2018 18:03
Show Gist options
  • Save gwang/68aaa878da77985fc8ac to your computer and use it in GitHub Desktop.
Save gwang/68aaa878da77985fc8ac to your computer and use it in GitHub Desktop.
Git tricks
  1. Remove a file/directory in the repo but not locally:
    1. git rm --cached filepath
    2. git rm --cached -r directory_path
  2. To undo adding a file or directory:
    1. git reset $file_or_dir_name
  3. Start a bitbucket repo with existing project in local directory
    1. create a repo with ${repo_name} in bitbucket web
    2. cd your_project_path
    3. git init
    4. git remote add origin git@bitbucket.org:${user_name}/${repo_name}.git
    5. do git operations as needed
      • for example, git pull origin master
    6. git commit -m ${commit_message}
    7. git push -u origin master
  4. Show command history in a Windows cmd window DOSKEY /history
  5. Remove from repo but keep the local files: git rm --cached -r [dir|file]*
  6. Make sure your ssh works
    1. ``eval `ssh-agent -s```
    2. then do ssh-add
    3. then verify what has been added using ssh-add -l
    4. You will also be able to test out the connection using ssh -T git@bitbucket.org
    5. Download myssh1.zip from Google email
    6. Unzip and move the content of the zip file over to ~/.ssh.
    7. Make sure the id_rsa permission is right: chmod 400 id_rsa.
  7. Git ignore file for java project:
# Eclipse
.classpath
.project
.settings/

# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
log/
target/

If you place this in .gitignore the root directory of your project, the ignore settings will be applied to all sub-directories automatically.

  1. With Intellij IDE
    • Actually, once you are done to step #2.4, the IDE will pick up the Git integration, and you can do all the add, commit, and push operations within the IDE.
  2. How to undo almost everything in Git
  3. Protocols to choose from when cloning
  4. Change remote url of a local git repo:
git remote rename origin backup
git remote add origin git@company:user/repo.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment