Skip to content

Instantly share code, notes, and snippets.

Last active Jul 27, 2020
What would you like to do?
Git Basics

Git Basic Commands

$ git init                               # initializes the repo. need to be at the root of the project
$ git add . OR git add -A                # all all the files, staged 
$ git status                             # shows all the files that are ready to be committed; staged
$ git commit -m 'first project version'  # initial commit
$ git remote add origin<<username>>/<<repository_name>>	# sets up GitHub as the origin for the master branch
$ git remote -v                          # shows remote origin
$ git push -u origin master              # this pushes the repo into GitHub. always push the origin master. Only first time push

$ git remote add <remote-name> <url>     # Add different remote
$ git subtree push --prefix <directory> <remote-name> master            # Push a subfolder on the master branch

$ git stash                              # Discard all local changes, but save them for possible re-use later
$ git checkout -- <file>                 # Discarding local changes (permanently) to a file
$ git reset --hard                       # Discard all local changes to all files permanently
$ git pull                               # Get latest changes

$ git clone <url>.                       # Clone a repo
$ git clone -b <branch-name> <url>       # Clone a repo's branch

$ git commit --amend                     # Amend last commit message
$ git push --force                       # Push the amended message


$ git branch                      # will tell us what brnacehs exists
$ git checkout -b <<branchName>>  # creates a new branch and switches to it
$ git checkout <<branchName>>     # switch brances
$ git merge <<branchName>>        # need to be on the branch we want to merge into (in this case master)
$ git branch -d <<branchName>>    # delete a branch


$ git --version
$ git config --global "Esau Silva"
$ git config --global
$ git config --global core.editor code -w
$ git config --global checkout
$ git config --global branch
$ git config --global alias.ct commit
$ git config --global status
$ git config --list

Update Forked Repo

Add the remote, call it "upstream":

$ git remote add upstream

Fetch all the branches of that remote into remote-tracking branches, such as upstream/master:

$ git fetch upstream

Make sure that you're on your master branch:

$ git checkout master

Rewrite your master branch so that any commits of yours that aren't already in upstream/master are replayed on top of that other branch:

$ git rebase upstream/master

If you don't want to rewrite the history of your master branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/master. However, for making further pull requests that are as clean as possible, it's probably better to rebase.

If you've rebased your branch onto upstream/master you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:

git push -f origin master

You only need to use the -f the first time after you've rebased.

Update Author

Interactive rebase off of a point earlier in the history than the commit you need to modify (git rebase -i <earliercommit>). In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:

git commit --amend --author="Author Name <>"

For example, if your commit history is A-B-C-D-E-F with F as HEAD, and you want to change the author of C and D, then you would...

  1. Specify git rebase -i B (here is an example of what you will see after executing the git rebase -i B command)
    • if you need to edit A, use git rebase -i --root
  2. Change the lines for both C and D from pick to edit
  3. Once the rebase started, it would first pause at C
  4. You would git commit --amend --author="Author Name <>"
  5. Then git rebase --continue
  6. It would pause again at D
  7. Then you would git commit --amend --author="Author Name <>" again
  8. git rebase --continue
  9. The rebase would complete.
  10. Use git push -f to update your origin with the updated commits.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment