Skip to content

Instantly share code, notes, and snippets.

@TomGalla11
Created October 26, 2020 12:21
Show Gist options
  • Save TomGalla11/c22c128254fbaee500b7f09a256cdc0f to your computer and use it in GitHub Desktop.
Save TomGalla11/c22c128254fbaee500b7f09a256cdc0f to your computer and use it in GitHub Desktop.
Basic git commands

Useful Links

https://github.com/git-tips/tips

https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html

https://gist.github.com/eashish93/3eca6a90fef1ea6e586b7ec211ff72a5

Code Examples

# Check all branches

git branch -a

# See difference between current files and previously commited version

git diff <filename>
git difftool <filename> # Launch with external diff tool

# Adds your modified files to the queue to be committed later. Files are not committed. 
# When you commit it's only going to commit the changes in the index (the "staged" files).

git add <filename>
git add .

# Commit with message

git commit -m "Commit message"

# Check status of current branch

git status

# Check log with graph
# Press `q` to exit

git log --all --decorate --oneline --graph

# Create alias for the log check - run with `git l`

git config --global alias.l 'log --all --decorate --oneline --graph'

# Push

git push
git push origin <branch name> # Specify origin branch name

# If you haven't connected your local repository to a remote server, add the server to 
# be able to push to it:

git remote add origin <server>

# Instead, to drop all your local changes and commits, fetch the latest history from the 
# server and point your local master branch at it, do this:

git reset --hard origin/master
git reset --hard HEAD # Alternative

# Create a new branch

git branch <branchname>

# Create a new branch and switch to it:

git checkout -b <branchname>

# Switch from one branch to another:

git checkout <branchname>

# See status of unpushed commits

git log origin..

# Pull in the latest changes from origin.

git pull
git pull origin <branchname> // Alternative. This automatically merges

# Alternative option to pull in latest changes. Note - `git fetch` is very important,
# and you need to run it before checkout your local situation against its remote. This 
# is because `git fetch` is what gives your local repo the details on what's happening remotely.
# It doesn't hurt your local repo at all.
# https://stackoverflow.com/a/6884809/8014660

git fetch // Grabs changes but doesn't merge them into your code
git merge // Actually merge the fetch into your code

# Remove untracked files from the working tree

# Print out the list of files which will be removed (dry run)
git clean -n

# Delete the untracked files from the repository (including folders)
git clean -f -d

Additional options

I like to keep all my gits in a single parent folder. To check the status of them all quickly and with one command, create a file in the parent directory called git-status. Put the code from this gist into that file. Then you can simply call the file with bash git-status. There you go - an easy way to check the status of multiple projects.

Note - I added the following to line 35 to print the current git banch:

echo "On branch:"
echo "$(git branch)"

Additional aliases

I use this to quickly commit and push my develop changes, merge up to my master and push, and then checkout back out my develop branch to continue working. This goes in the [alias] portion of your ./gitconfig file. It allows you to pass in a string for your commit message. You call it like this: git up-dm "My commit message here". In my system, dm stands for my develop and master branch.

[alias]
    up-dm = "!f(){ git add . && git commit -m \"$1\" && git push && git checkout master && git merge develop && git push && git checkout develop; };f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment