Git workflow
Home This is documentation on how to use Git (Gitlab) commands.
Table of Contents
- Disclaimer
- Git is a complicated tool and there might be mistakes born due to my incorrect understanding of how git works. Let me know if you have any questions, or if things don't work out the way I say they would.
1. Requirements
- Terminal or Command-Prompt
- Make sure git is installed on your computer.
2. For setting up the repository on your local machine
- Note
Do not use
git clone ..
if you are cloning a Lean-specific repository. Useleanproject get ...
instead.You need to follow these steps only the first time.
- (no term)
- In your local machine, navigate to a location where you wish to create a local copy of the repository.
- (no term)
- Run the command:
git clone https://github.com/vaibhavkarve/samplerepo.git
.- This will create a copy of the remote GitLab repository (called
origin
) into your local machine (in a folder calledsamplerepo
). - It will also create a hidden
.git
folder withinsamplerepo
. - You might have to enter your username and password here for authorization.
- You now have a working copy of the repository including all its branches.
- (To undo this step, simply delete the folder from your local machine.)
- This will create a copy of the remote GitLab repository (called
3. Helpful commands that you can run at any time without it messing up your files
git fetch
git branch
git status
git log --oneline --decorate --graph --all
4. If now you wish to change some part of the code
(You need to repeat these steps every time you start working on the code.)
4.1. Fetch first
Always start by running the command git fetch
.
- This updates your local repository with any new changes that anyone else might have pushed to GitLab since the last time you updated the repository.
- This command is perfectly safe to run as it does not overwrite anything and causes no loss of data. It is just a way of syncing your git history with that of the remote repository.
- Run this as often as you wish, even while in the middle of working on something. The more the better.
4.2. Create a branch
- Before you start editing anything, create a new branch. This keeps
any changes you make out of the master branch till you are ready to
merge in the new changes.
- Run the command
git branch dumbidea
to create a branch calleddumbidea
. (Use a more descriptive name though). - To check if this worked, run
git branch
to create a list of branches with an asterix next to the branch you are currently on. - Note that the default branch is called
master
. - (You can undo this step by running
git branch -d dumbidea
. This will delete the branch you just created.)
- Run the command
- Switch to the branch you created in the previous step by running
git checkout dumbidea
.- (You can undo this step and switch back to master by running
git checkout master
). - Important: every time you switch branches, the files on your local machine will change to reflect the state of the files in that particular branch.
- (You can undo this step and switch back to master by running
4.3. Make changes
Make the changes you wish to make to any files you want. Say you
changed A.txt
, B.txt
and C.txt
.
- If at any point you wish to revisit or view the old file (before
your changes), you can access the old version simply by switching
back to master branch (
git checkout master
). Make sure you switch back to yourdumbidea
branch before making more changes. - You can review which files you have changed by running
git status
. - You can review which changes you made in a particular file by
running say
git diff C.txt
. - If you are dissatisfied by the changes you made to say
C.txt
, simply rungit checkout C.txt
to revert back to the state of the file as it was during the previous commit. This will undo any changes. (You cannot undo this undo-ing. So be careful. Run thegit checkout C.txt
command only if you are sure you want to discard any changes you made.)
4.4. Stage and commit
- Stage the changes you do wish to keep. This is done by running
git add path/to/file
- Throughout the following steps, you can always run
git status
to keep track of which files have been modified, which have been staged, and which have been committed. - Staging the file is like saying "I am happy with the changes I have made and would like to start the process of creating a recorded history for the files at this point."
- (You can undo this step by running
git reset HEAD path/to/file
. - If you are done editing
A.txt
but are still working onB.txt
, it is okay to stage onlyA.txt
. Even if you editA.txt
after staging, you can always re-stage it by typinggit add A.txt
.
- Throughout the following steps, you can always run
- Once all files have been staged, we are ready to commit our changes
by running
git commit dumbidea
:- This is a local commit! This will only commit on our local
machine and will not affect what is present on GitHub. Think of
this as an extra layer of security before your
dumbidea
start affecting everyone else. - You will be prompted with a message window where you should type out your commit message in as meaningful a way as possible.
- Check that your commit was successful by running
git status
. You should now see a message that saysyour branch is 1 commit ahead of origin/master
. - (If you wish to undo this commit, look at the next to steps depending on whether you wish to do a soft or hard undo).
- This is a local commit! This will only commit on our local
machine and will not affect what is present on GitHub. Think of
this as an extra layer of security before your
- If you decide that you committed the file too soon and would like
to undo just the commit but keep all the edits you might have made
to the file:
- Run
git reset HEAD~1
- This takes you back to how things were two steps ago i.e. files are staged but not committed.
- (To undo this undo-ing, simply repeat the previous step.)
- Run
- If you decide that the commit you made in the previous step is
complete rubbish and you want to erase all history of the edits
that were made since the commit before that, you need a hard
reset.
- Run
git reset --hard HEAD~1
- (This undo-ing is hard to undo, but possible. Just be careful before doing a hard reset.)
- Run
4.5. Push
We are now happy with our commit and now we would like everyone to be able to see it on GitHub
- If you are done with the commit and haven't had the need to undo
it, then you can run
git push
to push your changes to the remote repository (i.e. this will updateorigin
). - (If you wish to undo this, it is going to be difficult, but not
impossible. First, inform everyone on your team to hold off and
not work with your erroneous files. Next, start Googling for
ways to undo the
git push
commands.)
4.6. Repeat
Now you can repeat the pull-edit-add-commit-push loop as many times
as you want in order to make further changes to your dumbidea
branch.
- Keep running
git fetch
in order to remain up-to-date with GitHub.
4.7. Merge
- Once you are completely confident that
dumbidea
is actually not-so-dumb-afterall, you can decide to merge it into themaster
branch.- Switch to master branch by running
git checkout master
. - To merge your branch with master, run
git merge dumbidea
. - If this process works out smoothly without displaying any Conflict messages, then you have merged successfully. If not, look at the next step.
- Switch to master branch by running
- If some other user (or you yourself) have made changes to the
master branch that are not compatible with changes that you have
made to files in
dumbidea
then Git will automatically detect this as a conflict and not allow you to merge till you resolve all the conflicts. If this happens, do the following:Open the files that Git has indicated as having conflicts in them, say
A.txt
. InA.txt
, Git will have marked the specific lines that are in conflict as follows:<<<<<<< HEAD:A.txt This is the line that was in the master branch.
This is the line that is in the dumbidea branch.
>>>>>>> dumbidea:A.txt
- Replace the block of text specified above with either the
version in
master
branch, the version indumbidea
branch, or a mixure of both versions. - Make sure to completely remove the <<<<<<<,
=====
and >>>>>>> lines. - After resolving each section in each conflicted file, run
git add
on each file to mark it as resolved and to stage it. - Run
git commit
without any conflict messages.
- Replace the block of text specified above with either the
version in
4.8. Cleanup
- Cleaning up after you are done using your branch.
- After you have successfully merged a branch into
master
, you can get rid of thedumbidea
branch by runninggit branch -d dumbidea
. - This will clean up the branch history.
- You can check that the branch has been deleted by running
git branch
.
- After you have successfully merged a branch into
- Next time you want to edit something, start again by creating a new branch.