Git
Here are somes git tips & tricks
Using Git
The following commands can be helpful for working with git.
| git command | Comment |
|---|---|
| git init | Initialize a directory as git managed repository |
| git clone <repo_url> | Clone a remote repository to your local client |
| git status | Shows uncommited changes, new files etc. |
| git add <wildcard_or_filename> | Stage an updated / new file to the next commit |
| git rm <wildcard_or_filename> | Remove a file and stage the removal for the next commit |
| git commit -m “<commit message”> | Commit staged changes under a new commit |
| git commit | Will open an editor to write more descriptive commit messages. See here for a guide on good commit messages |
| git checkout <branch_name> | Switch to another branch |
| git branch | Shows a list of existing branches |
| git branch <branch_name> | Creates a new branch (from the currently checked out branch) |
| git merge <branch_name> | Merge changes from branch_name to the currently checked out branch |
| git push | Push commited changes to the remote repository |
| git pull | Pull current state from the remote repository to your local repo |
Working with git-flow
Git-flow assists you by combining multiple steps of git commands to one git-flow command
which will do a workflow of steps. Although git-flow makes live easier in some cases,
it makes it also more complex sometimes and you need to execute some steps before or after using
a git-flow command as regular git command. (See below)
As an example, here is the comparison between the regular git commands and the appropriate
git-flow command for creating a release.
| git-flow command | git command |
|---|---|
| git-flow feature start <feature_name> | git checkout -b feature/<feature_name> develop |
| git-flow feature finish <feature_name> [–squash] | git checkout develop |
| git merge [–squash] –no-ff feature/<feature_name> | |
| git branch -d feature/<feature_name> |
Another git-flow cheat sheet can be found here.
Config
Username
Add the username to your profile
git config --global user.name "sboistel"Add your email to your profile
git config --global user.email "[sboistel@mail.com](mailto:sboistel@mail.com)"Credentials
Store your repository login credentials
git config credential.helper storeBranches
List branches
git branchCheckout
Check which branch you are currently on
git checkoutSwitch branches:
git checkout nom_brancheManage
Push
Push to a branch other than master
git push -u origin nom_brancheMerge
Switch to the master (main) branch, then
git merge nom_brancheDon’t forget to push afterwards
git pushStash
Stash allows you to keep local changes without propagating them.
You can check what can be saved:
git stash listClear the stash list
git stash clearApply or commit a stash
git stash apply
git stash popRebase branch
Go to the main/master branch
git checkout mainPull changes
git pullGo back to your branch
git checkout yourbranchnameLet’s rebase it
git rebase mainThen open your file and resolve any conflicts.
Finally, continue the rebase
git rebase --continueUpload
git push --force