Git basic commands everyone should know


Full article


Yes, we have GUI now days, I love them and use them too, but there are times where you are working on a team in a real world and need to write some lines. It's better if we learn just a couple of them or at least have them handy.

Show settings

git config --global -l

Add your information

git  config --global 'myUser'
git  config --global 'myEmail'

Compatibility (This allows multi OS team collaboration)

git config --global core.autocrlf true

Change default core editor

# Mac and Linux:
git config --global core.editor "subl -n -w"
# Windows:
git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

# To edit a file simply add --edit, example:
`git config --global --edit`

Generate SSH Key

ssh-keygen -t rsa -b 4096 -C "githubEmail"

# Mac exaxmple location:
cat /Users/Arturo/.ssh/
# Windows example location:
# `/h//.ssh/id_rsa`

Working with Git


# (Local)
git checkout branchName
# (Remote)
git checkout --track origin/branchName

Discard ALL changes (you cannot undo this)

git checkout -f
# Or
git checkout HEAD^
git checkout -f master

Shows commit history

# (command line)
git log --graph --oneline -- decorate --all
# (graphical)
gitk --all

Renaming and Deleting

git rm file1	        # Remove file
git mv file1 file2	# Renames file

How to collaborate on a project and submit pull requests:

# 1. Clone the desired repo.
# --------------------------
# 2. List configured remotes of the new clone: 
git remote -v
# 3. Add upstream to the desired repo that you cloned:
git remote add upstream https://githubURL
# 4. Fetch the branches and their respective commits from the upstream repository. 
# (Commits to master will be stored in a local branch, upstream/master.)
git fetch upstream
# 5. Check out your fork's local master branch.
git checkout master

# I suggest that every morning before working on your project you switch to master and do a:
git pull upstream master

# Create pull request:
# --------------------

# Terminal walkthrough:
# On the terminal (make sure you are on your branch) go to the path of your forked repo and type:
git status
# that will allow you to see what's been changed on your current branch. 
# You can select the files you'd like to stage by typing (for this example I'll use as the file we want to commit):
# This stages the file
git add
# This commits the files added with a message
git commit -m "Added FileUploader versioning" 
git push origin nameOfYourBranch

Now go to your forked repo on github (Web) and on the right corner click on Pull Request, you are going to see your commited changes. You will see an image like the one below (by default if the same branch exists on the origin, it will autopopulate, if it doesn't, then you can select it) which means that tab#1 is the origin repo, tab#2 is where you are going to request the merge, tab#3 is your forked repo and tab#4 from where you are pushing.