Useful Git Commands

  • $ git init
    #initialize an empty Git repoistory
  • $ git status
    #see the current state of the project
    untracked file: it is a new file and not be tracked by Git
    staged:Files are ready to be committed.
    unstaged: Files with changes that have not been prepared to be commited.
    untracked: Files aren’t tracked by Git yet. This usually indicates a newly created file.
    deleted: File has been deleted and is waiting to be removed from Git.
  • $ git add <filename>
    $ git add -A .
    # add all, “.” stand for the current directory
    $ git reset <filename>
    #remove a file or files from the staging area
  • $ git commit -m “a message about the changing”
    # Staging Area: A place where we can group files together before we “commit” them to Git.
  • $ git add ‘*.txt’
    # Wildcards: need quotes
  • $ git log
    # see all the changes
    $ git log –summary
    # see more information for each commit
  • $ git remote add origin https://github.com/…
    # push local repository to the GitHub server, “origin” is the name for remote repository
  • $ git push -u origin master
    # push local changes to the “origin” repository
    # -u : remember the parameters, so that next time simply run git push
  • $ git pull origin master
    # check for changes on repository and pull down any new changes by running
  • $ get diff HEAD
    # diff of most recent commit using HEAD pointer
    # HEAD: a pointer that holds your position within all different commits. By default HEAD points to most recent commit, so it can be used as a quick way to reference that commit without having to look up SHA.
  • $ git diff –staged
    # –staged : see the changes you just staged.
  • $ git reset <filename>
    # unstage files, but not delete the files
  • $ git checkout — <target>
    # change the file back to the last commit
    # — : promising the command line that there are no more options after the ‘–‘.
  • $ git branch <new branch name>
    # create a new branch
  • $ git checkout <branch>
    # switch to the branch
    $ git checkout -b <branch>
    # create and checkout a new branch at the same time
  • $ git rm <filename>
    # remove a file
    $ git rm ‘*.txt’
    # remove all file
    $ git rm -r <folder>
    # remove a folder and all its content
  • $ git commit -am “message”
    # if you happen to delete a file without using ‘git rm’, then you still have remove it from the working tree with the command “git rm”. ‘-a’ option means auto removes deleted files.
  • $ git checkout master
    # switch to master branch
  • $ git merge clean_up
    # merge the branch
  • $ git branch -d <branch name>
    # delete branch: -d means if something isn’t merged, it will not allow be deleted
    # –force (-f) : force to be done
    $ git branch -D <branch name>
    # -D combines -f -d into one command
  • $ git push
    # push everything to remote repository
  • $ git add file && git commit -m “removed merge conflicts”
    or
    $git add file && git commit -m “resolved merge conflicts”
    # If pull failed (When conflict with your local file happens), you need this command
  • $ git push origin master –force
    or
    $ git pull –rebase
    $ git push
    # If push failed
  • $ git log origin/master..HEAD
    # Viewing Unpushed Git Commits
    $ git diff origin/master..HEAD
    #  view the diff using the same syntax
    See: Viewing unpushed git commits?
  • git reset –soft HEAD~1
    # Delete the most recent commit, keeping the work you’ve done
    $ git reset –hard HEAD~1
    # Delete the most recent commit, destroying the work you’ve done
    See: How do I delete unpushed git commits?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.