Basics

I wrote about git before (for GitHub use). There is a lot more to git. Below is a list of some of the important commands. I relied on this youtube series for this post.

Set User

git config --global user.name 'name'
git config --global user.email 'email'

Stop tracking

rm -rf .git

Remove files from the staging area

git reset

Git info on the git repository

git status # which files changed
git diff   # shows the chances
git log    # commits history
git log --stat # shows the files that were changed in the commit
git remote -v # where is the remote
git branch #-a, shows branches
git reflog # commits history

Create a branch and switch to it

git branch branch1
git checkout branch1

To push that branch to remote

git push -u origin branch1

This associates the local branch to the remote branch.

Merging changes to master

git merge branch1

Undoing changes

If nothing has been committed yet,

git checkout

Bad Commits

Before it Reaches others

If (careful!) not pulled by others yet

git commit --amend -m "message"

That changes the hash of the commit.

After others have Pulled it

This takes you back to a previous commit but creates a new commit in the history

git revert hash1
Committing to the Wrong Branch

Switch to the correct branch (using git checkout branchX then

git cherry-pick hash1

where hash1 is the commit that went to the wrong branch. To delete from the wrong branch, switch back to that branch then

git reset --soft hash1

or

git reset --hard hash1 # deletes changes to the tracked files
git clean -df # removes changes to files that were not added to the staging area

It’s really important to keep the git messages.

Inspecting Previous Commits

git reflog
git checkout hash1

Entering the “detached-head” state, where we are not on a branch.

git branch branch2

creates a branch from that commit, I think.

Comparing Changes

This compares two commits

git diff hash1 hash2

Comparing Two Random Folders

git diff --no-index dir1 dir2

Git over SSH

ssh://192.168.xx.xx:8022/data/data/com.termux/files/home/folder

where folder is a git repository.

Common Scenarios

Pushing to an Updated Repository

You update a repository locally and don’t commit. Somebody updates the remote repository and now you want to push your local changes. In order to do that, you first try git pull, and you get an error message.

error message

Following the advice given here

First, I think if you were to git add then git commit, your local repo would be clean enough to do a git pull. However, if there may be changes you don’t want to commit until you see what happened on the upstream, you can use git stash. It will temporarily clean up your working directory and save your changes, so you can pull (I would recommend git pull --rebase to avoid merge points - but it is a personal taste issue). Once you have upstream changes pulled, you can get your local modifications back using git stash pop. After you clean up conflicts and get rid of unnecessary changes, you can add, commit, then finally push.

git stash

This saves the local changes and cleans the tree.

git pull --rebase 

Pulls the changes from the remote. The rebase

git stash pop

Then you will probably need to resolve merge conflicts. Git will summarize the changes to the file this way

If you have questions, please
<<<<<<< UPDATED UPSTREAM
open an issue
=======
ask your question in IRC.
>>>>>>> STASHED CHANGES
More stuff.

Remove the <<<<<<<, =======, >>>>>>> markers, make sure the file is in the version you want, then push.