Git
Basics Branching and Merging:
Create a new branch
git branch new_branch
Switch to the new branch
git checkout new_branch
Create and switch
git checkout -b new_branch
Switch back to master
Before doing that, note that if your working directory or staging area has uncommitted changes that conflict with the branch you’re checking out, Git won’t let you switch branches. You have to commit your changes, or get around this by stashing.
git checkout master
Merge a branch
To do that, you have to be in the branch you want to merge into ! For example, to merge new_branch
into master
git checkout master
git merge new_branch
When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward. This is called “fast-forward”.
Delete a branch
After merging, you can delete the branch merged since the other one points at the same place.
git branch -d new_branch
Merge conflicts
If you changed the same part of the same file differently in the two branches you’re merging, Git won’t be able to merge them cleanly and you will get a merge conflict.
If the conflict cannot be solved automatically, Git will pause the process while you resolve the conflict. To see which files are unmerged at any point after a merge conflict you can run git status
.
Git adds standard conflict-resolution markers to the files that have conflicts:
<<<<<<< HEAD:index.html
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
=======
At urna condimentum mattis pellentesque id. Leo vel fringilla est ullamcorper eget.
>>>>>>> new_branch:index.html
This means the version in HEAD
(the branch you merge into) is the top part of the block (everything above the =======
), while the version in the new_branch
is the bottom part. You can either choose one side or the other or merge the contents yourself.
After resolving each of these sections in the conflicted files, you will have to run git add
on each file to mark it as resolved.
You can run git mergetool
to use a graphical tool to resolve the conflicts. After exitting the merge tool, Git will ask if the merge was succesful. If you tell the script that it was, the file will be staged for you.
Once all the conflicts have been resolved and staged, you can git commit
to finalize the merge.
Stashing the working directory
Pushing a new stash
If you want to switch branches to work on something else, but you don’t want to commit half-done work, the answer is git stash
or git stash push
. It will save the full state of your working directory to revert to the last commit, and you can new switch branches.
Stashes stored
To see which stashes you’ve stored, you can use:
git stash list
Restore a stash
To reapply a stash, you can use git stash apply
. To apply an older stash, you can specify it by naming it : git stash apply stash@{x}
. If you don’t specify a name, Git assumes the most recent stash.
After applying a stash, Git re-modifies the files reverted when you saved the stash.
Having a clean working directory and applying it on the same branch aren’t necessary. You can apply the changes on another branch and if you have modified and uncommitted files in the working directory when you apply, Git will try to merge it automatically.
Remove a stash
Applying a stash will not remove it from the stack. You can remove it with git stash drop
with the name of the stash to remove: git stash drop stash@{x}
You can also run git stash pop
to apply the stash and immediately remove it from the stack.
In-depth stashing
By default
git stash
will stash only modified and staged tracked files. To include untracked files in the stash being created, you have to specify--include-untracked
or-u
. To include explicitly ignored files as well, use--all
or-a
.With the
--patch
flag, Git will not stash everything that is modified but will instead prompt interactively which of the changes to stash or keep in the working directory.
Cleaning the working directory
To get rid of the changes in the working directory and revert to the last commit, you can use git clean
.
By default, the git clean
command will only remove untracked files that are not ignored.
Be careful with this command, there is often no retrieving the content of those files.
- A safer option is to run
git stash --all
to remove everything but save it in a stash. - You can run the command with the
--dry-run
or-n
option to see what is going to be removed without removing them now. If you don’t know what thegit clean
is going to do, always run it with-n
first to double check before doing it for real. - You can also run the command in interactive mode with the
-i
flag to be extra careful with the process. You will go through each file individually or specify a pattern for deletion. - If the Git configuration variable
clean.requireForce
is set to true, you will have to add the flag-f
to use the command.
Remote connection
The convention for the main remote name is origin
. When cloning a repository from a remote server, the local version of the repository will automatically have a origin
connection to this remote server.
Adding a remote
If you create a local repository, you have to specify and add the remote connection yourself.
You can add a new remote connection for a local repository using :
git remote add remote_name remote_url
For instance: git remote add origin https://github.com/username/example.git
Checking remotes
By default, git remote
lists the current remote connections only by name. You can show the remote connections URLs with : git remote -v
Removing remotes
You can disconnect your local repository to a remote with git remote remove remote_name
. This will have no effect on the remote repository.
Rename remote
You can rename the remote connection with git remote rename old_name new_name
.
More resources
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging