Igor Simic
6 years ago

Git Commands

Here is a couple of terminal git commands for everyday use:)

  • How to connect existing folder (with files) to newly created repo
    • create repository directory and go to that folder in iterm/terminal
    • git init --bare
    • navigate to folder where your files are and make available for git
    • git init
    • connect that folder with repo
    • git remote add origin <path to repo URL>
    • commit all files from this folder
    • git add --all
    • commit these files
    • git commit -m "initial commit"
    • push for the first time
    • git push origin master
      // after that you can use 
      git push

  • Show files from last commit
  • git show --stat --oneline HEAD
    // http://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit

  • Connect local folder with remote repository:
  • git init
    git remote add origin <remote repo url>
  • Set username and password for remote repo
  • git remote set-url origin https://name:[email protected]/repo.git

  • remove git from folder but keep your files
  • rm -rf .git

  • git pull and overwrite local files (force pull)
  • git fetch --all
    git reset --hard origin/master
    git pull origin master

Remove file from commit

  • git update-index --assume-unchanged <file_name>
Revert that:
  • git update-index --no-assume-unchanged <file>

git remove file from comit
  • git checkout filename

git compare files
  • git diff -- myfile.txt

see remote repositories
git remote

push remote branch
git push remoteBranchName

what is going to be pushed
git cherry -v

show remote repo URL

git remote show origin

How to create a new branch, push to it and merge with master

1. Create new branch
git branch test_branch
2. go to that new branch
git checkout test_branch
 to check are you on this new branch you can execute this command:
git branch
output should be something like this:
* test_branch
// notice a star next to active branch
3. commit your changes to new branch
// to see changes available for commit
git status 

// add modified files for commit
git add --all

// commit changes with message
git commit -m "my commit message"

// push the changes to your new branch
git push

4. merge with master
// switch to master
git checkout master

// pull your changes from new branch
git pull origin test_branch

// merge and commit
git commit -m "merging branches test_branch and master"

5. push to master
git push master