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 popMore 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 MyNewBranchDelete local branch¶
$ git branch -d MyLocalBranchCreate 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_branchMerging¶
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-featureRemote¶
https://
Push local repository to existing remote¶
$ git remote add origin git@github.com:USERNAME/REPOSITORY.git
$ git push --all --set-upstream originShow 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://
Add a submodule¶
git submodule add git@github.com:USERNAME/REPOSITORY.git PATH/TO/SUBMODULEDIRDownload files to empty submodule directory¶
When cloning a repository with submodules and the submodule directories are empty:
git submodule update --init --recursiveRemove 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/nullMisc¶
Remove files from the index without removing them from disc¶
If you forgot to add a file to .gitignore:
$ git rm -rf --cached file-nameThe 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¶
- Git reference manual
- Pro Git book (2nd, 2014)
- Git for beginners on stackoverflow
- git - the simple guide
- The beginner’s guide to contributing to a GitHub project
- On undoing, fixing, or removing commits in git
Interesting discussions on Git¶
- A successful Git branching model
- How to Write a Git Commit Message
- Code Reviews: Before You Even Run The Code
- What is the benefit of gits two stage commit process
- Why would I want to stage before committing?