GIT

Git Quickstart Guide

Article by:
Date Published:
Last Modified:

Below is a quick start guide (aka cheet sheet) for using git.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Create a new empty repo in a folder
# (folder does not have to be empty)
$ git init

# Clone a remote repo to local location
$ git clone https://github.com/username/repo-name ./local-repo-folder

# Add changes (verbose flag helps!)
# Note that this does not track deleted
# files, use "git add -u -v" below 
$ git add . -v

# Use to track deleted files
# ("git add ." does not do this)
$ git add -u -v

# Stage all changed files, and show which files were staged
$ git add -Av

# Interactive change-by-change staging
$ git add -p

## Commit to repo
$ git commit -m "Commit message."

## Add remote push location to Github (called "origin") for current repo
# Note that this is the https method, and will require you to 
# enter your password on every push.
$ git remote add origin https://github.com/username/repo-name

# Modify an existing push/pull location (in this case origin)
$ git remote set-url origin https://new-url

# Remove remote push location added in previous command (in this case origin)
$ git remote rm origin

# Delete local branches that are not present on remote
$ git remote prune origin
$ git remote prune origin --dry-run # Only list them, don't delete

# Push to Github repo added in above command
$ git push origin

# Push a specific local branch to a specific remote branch
$ git push origin my_local_branch:my_remote_branch

# To add a lightweight tag to the latest commit
# (in this example I am adding a version number)
$ git tag v2.1.0.5

# To remove the lightweight tag added above
$ git tag -d v2.1.0.5

# Push tags to remote repo location assigned above ("gh")
# (these are not pushed by default, and this does not
# push deleted tags, see below)
$ git push origin --tags

# Since I normally always want to push tags at the same
# time as I push commits, I combine the two commands into
# one line like so:
$ git push origin; git push origin --tags

# To push deleted tags to a remote location
# (so it deletes the tags at the remote)
$ git push origin :refs/tags/v2.1.0.5

# Show git branches
$ git branch # Local
$ git branch -r # Remote
$ git branch -a # Local and remote

# Create a new branch and checkout
# to begin working on it
$ git checkout -b my-new-branch

# List all tags in repo
$ git tag

# Get repo info
$ git status

# Add a new sub-module to existing repo
# Note that the local install path is relative to the root directory
# of the repo
$ git submodule add https://path.to.remote.repo ./local/install/path

# To revert all uncommitted changes to modified files and delete untracked 
# files/directories (useful if you done edits which you no longer wish to 
# keep). Calling git reset --hard will print the commit the repo falls back to.
$ git reset --hard
$ git clean -fd

# To print the SHA-1 (long version) of the current commit
$ git rev-parse HEAD

# To print changes made in the last two commits (useful to get
# back up to speed on what you're working on)
$ git diff HEAD~2

Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Related Content:

Tags

comments powered by Disqus