Skip to article frontmatterSkip to article content

Git

Setup and Config

Show or change Git username or email address

$ git config --list # repository-specific settings
$ git config --list --global # global git settings
$ git config user.name "Enrico Pallazzo"
$ git config user.email "enrico.pallazzo@lapd.com"

The global settings are stored in the Git config file in the HOME directory (~/.gitconfig), repository-specific settings are found at .git/config in the respective repository folder.

Branches

Change branch without committing changes: stash and pop

Want to have a look at another branch without committing changes done so far? Put them in a stash where they can hide until you switch back.

$ git stash # on the original branch
$ git checkout other-branch
# do some stuff on the other branch
$ git checkout original-branch
$ git stash pop

More about git-stash.

Create a local branch, push it to a remote repository and track it

$ git checkout -b MyNewBranch # create and switch to new branch
# do some stuff
$ git push -u origin MyNewBranch

Delete local branch

$ git branch -d MyLocalBranch

Create empty branch with no history

Everyone will tell you not to do this. But hey, you can do whatever you want in your own repositories, so do this:

git switch --orphan my_new_branch

# commit some files, like a README or .gitignore

git push -u origin my_new_branch

Merging

Merge master into feature branch before making a PR

$ git checkout master
$ git pull
$ git checkout new-feature
$ git add *files-and-changes*
$ git commit -m "feature description"
$ git reset HEAD --hard # removes all uncommited files
$ rm *untracked-files*  # to prevent merge conflicts
$ git merge master
# resolve potential merge conflicts
$ git commit -m "resolved mergeconflicts | merged master"
$ git push origin new-feature

Remote

https://git-scm.com/docs/git-remote

Push local repository to existing remote

$ git remote add origin git@github.com:USERNAME/REPOSITORY.git
$ git push --all --set-upstream origin

Show or change remote

In this example we switch from HTTPS to SSH:

$ git remote -v
> origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin  https://github.com/USERNAME/REPOSITORY.git (push)
$ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
$ git remote -v
> origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
> origin  git@github.com:USERNAME/REPOSITORY.git (push)

Submodules

https://git-scm.com/docs/git-submodule

Add a submodule

git submodule add git@github.com:USERNAME/REPOSITORY.git PATH/TO/SUBMODULEDIR

Download files to empty submodule directory

When cloning a repository with submodules and the submodule directories are empty:

git submodule update --init --recursive

Remove a submodule (leaving no trace)

git rm PATH/TO/SUBMODULEDIR
rm -rf .git/modules/PATH/TO/SUBMODULEDIR
git config -f .git/config --remove-section submodule.PATH/TO/SUBMODULEDIR 2> /dev/null

Misc

Remove files from the index without removing them from disc

If you forgot to add a file to .gitignore:

$ git rm -rf --cached file-name

The file is now untracked and can be added to .gitignore. Then, you can commit the deletion and the modified gitignore file.

Useful articles / resources

Basics

Interesting discussions on Git

GitHub